home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lisp / rmail.el < prev    next >
Lisp/Scheme  |  1994-09-10  |  86KB  |  2,343 lines

  1. ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs.
  2.  
  3. ;; Copyright (C) 1985,86,87,88,93,94 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: mail
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Code:
  25.  
  26. ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
  27. ;;   New features include attribute and keyword support, message
  28. ;;   selection by dispatch table, summary by attributes and keywords,
  29. ;;   expunging by dispatch table, sticky options for file commands.
  30.  
  31. ;; Extended by Bob Weiner of Motorola
  32. ;;   New features include: rmail and rmail-summary buffers remain
  33. ;;   synchronized and key bindings basically operate the same way in both
  34. ;;   buffers, summary by topic or by regular expression, rmail-reply-prefix
  35. ;;   variable, and a bury rmail buffer (wipe) command.
  36. ;;
  37.  
  38. (require 'mail-utils)
  39.  
  40. ;; For Emacs V18 compatibility
  41. (and (not (fboundp 'buffer-disable-undo))
  42.      (fboundp 'buffer-flush-undo)
  43.      (defalias 'buffer-disable-undo 'buffer-flush-undo))
  44.  
  45. ; These variables now declared in paths.el.
  46. ;(defvar rmail-spool-directory "/usr/spool/mail/"
  47. ;  "This is the name of the directory used by the system mailer for\n\
  48. ;delivering new mail.  It's name should end with a slash.")
  49. ;(defvar rmail-file-name
  50. ;  (expand-file-name "~/RMAIL")
  51. ;  "")
  52.  
  53. (defvar rmail-movemail-program nil
  54.   "If non-nil, name of program for fetching new mail.")
  55.  
  56. ;;;###autoload
  57. (defvar rmail-dont-reply-to-names nil "\
  58. *A regexp specifying names to prune of reply to messages.
  59. A value of nil means exclude your own name only.")
  60.  
  61. ;;;###autoload
  62. (defvar rmail-default-dont-reply-to-names "info-" "\
  63. A regular expression specifying part of the value of the default value of
  64. the variable `rmail-dont-reply-to-names', for when the user does not set
  65. `rmail-dont-reply-to-names' explicitly.  (The other part of the default
  66. value is the user's name.)
  67. It is useful to set this variable in the site customization file.")
  68.  
  69. ;;;###autoload
  70. (defvar rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|\
  71. ^received:\\|^x400-originator:\\|^x400-recipients:\\|^x400-received:\\|\
  72. ^x400-mts-identifier:\\|^x400-content-type:\\|^message-id:\\|^summary-line:"
  73.   "*Regexp to match Header fields that Rmail should normally hide.")
  74.  
  75. ;;;###autoload
  76. (defvar rmail-highlighted-headers "^From:\\|^Subject:" "\
  77. *Regexp to match Header fields that Rmail should normally highlight.
  78. A value of nil means don't highlight.
  79. See also `rmail-highlight-face'.")
  80.  
  81. ;;;###autoload
  82. (defvar rmail-highlight-face nil "\
  83. *Face used by Rmail for highlighting headers.")
  84.  
  85. ;;;###autoload
  86. (defvar rmail-delete-after-output nil "\
  87. *Non-nil means automatically delete a message that is copied to a file.")
  88.  
  89. ;;;###autoload
  90. (defvar rmail-primary-inbox-list nil "\
  91. *List of files which are inboxes for user's primary mail file `~/RMAIL'.
  92. `nil' means the default, which is (\"/usr/spool/mail/$USER\")
  93. \(the name varies depending on the operating system,
  94. and the value of the environment variable MAIL overrides it).")
  95.  
  96. ;;;###autoload
  97. (defvar rmail-mail-new-frame nil
  98.   "*Non-nil means Rmail makes a new frame for composing outgoing mail.")
  99.  
  100. ;;;###autoload
  101. (defvar rmail-retry-setup-hook nil
  102.   "Hook that `rmail-retry-failure' uses in place of `mail-setup-hook'.")
  103.  
  104. ;;;###autoload
  105. (defvar rmail-secondary-file-directory "~/"
  106.   "*Directory for additional secondary Rmail files.")
  107. ;;;###autoload
  108. (defvar rmail-secondary-file-regexp "\\.xmail$"
  109.   "*Regexp for which files are secondary Rmail files.")
  110.  
  111. ;; These may be altered by site-init.el to match the format of mmdf files
  112. ;;  delimiting used on a given host (delim1 and delim2 from the config
  113. ;;  files).
  114.  
  115. (defvar mmdf-delim1 "^\001\001\001\001\n"
  116.   "Regexp marking the start of an mmdf message")
  117. (defvar mmdf-delim2 "^\001\001\001\001\n"
  118.   "Regexp marking the end of an mmdf message")
  119.  
  120. (defvar rmail-message-filter nil
  121.   "If non nil, a filter function for new messages in RMAIL.
  122. Called with region narrowed to the message, including headers.")
  123.  
  124. (defvar rmail-reply-prefix "Re: "
  125.   "String to prepend to Subject line when replying to a message.")
  126.  
  127. (defvar rmail-display-summary nil
  128.   "If non nil, the summary buffer is always displayed.")
  129.  
  130. (defvar rmail-mode-map nil)
  131.  
  132. (defvar rmail-inbox-list nil)
  133. (defvar rmail-keywords nil)
  134.  
  135. ;; Message counters and markers.  Deleted flags.
  136.  
  137. (defvar rmail-current-message nil)
  138. (defvar rmail-total-messages nil)
  139. (defvar rmail-message-vector nil)
  140. (defvar rmail-deleted-vector nil)
  141.  
  142. (defvar rmail-overlay-list nil)
  143.  
  144. ;; These are used by autoloaded rmail-summary.
  145.  
  146. (defvar rmail-summary-buffer nil)
  147. (defvar rmail-summary-vector nil)
  148.  
  149. ;; `Sticky' default variables.
  150.  
  151. ;; Last individual label specified to a or k.
  152. (defvar rmail-last-label nil)
  153. ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
  154. (defvar rmail-last-multi-labels nil)
  155. (defvar rmail-last-regexp nil)
  156. (defvar rmail-default-file nil
  157.   "*Default file name for \\[rmail-output].")
  158. (defvar rmail-default-rmail-file (expand-file-name "~/XMAIL")
  159.   "*Default file name for \\[rmail-output-to-rmail-file].")
  160.  
  161. ;;; Regexp matching the delimiter of messages in UNIX mail format
  162. ;;; (UNIX From lines), minus the initial ^.  Note that if you change
  163. ;;; this expression, you must change the code in rmail-nuke-pinhead-header
  164. ;;; that knows the exact ordering of the \\( \\) subexpressions.
  165. (defvar rmail-unix-mail-delimiter
  166.   (let ((time-zone-regexp
  167.      (concat "\\([A-Z]?[A-Z][A-Z][A-Z]\\( DST\\)?"
  168.          "\\|[-+]?[0-9][0-9][0-9][0-9]"
  169.          "\\|"
  170.          "\\) *")))
  171.     (concat
  172.      "From "
  173.  
  174.      ;; Username, perhaps with a quoted section that can contain spaces.
  175.      "\\("
  176.      "[^ \n]*"
  177.      "\\(\\|\".*\"[^ \n]*\\)"
  178.      "\\)  ?"
  179.  
  180.      ;; The time the message was sent.
  181.      "\\([^ \n]*\\) *"            ; day of the week
  182.      "\\([^ ]*\\) *"            ; month
  183.      "\\([0-9]*\\) *"            ; day of month
  184.      "\\([0-9:]*\\) *"            ; time of day
  185.  
  186.      ;; Perhaps a time zone, specified by an abbreviation, or by a
  187.      ;; numeric offset.
  188.      time-zone-regexp
  189.  
  190.      ;; The year.
  191.      " [0-9][0-9]\\([0-9]*\\) *"
  192.  
  193.      ;; On some systems the time zone can appear after the year, too.
  194.      time-zone-regexp
  195.  
  196.      ;; Old uucp cruft.
  197.      "\\(remote from .*\\)?"
  198.  
  199.      "\n"))
  200.   nil)
  201.  
  202. ;; Perform BODY in the summary buffer
  203. ;; in such a way that its cursor is properly updated in its own window.
  204. (defmacro rmail-select-summary (&rest body)
  205.   (` (let ((total rmail-total-messages))
  206.        (if (rmail-summary-displayed)
  207.        (let ((window (selected-window)))
  208.          (save-excursion
  209.            (unwind-protect
  210.            (progn
  211.              (pop-to-buffer rmail-summary-buffer)
  212.              ;; rmail-total-messages is a buffer-local var
  213.              ;; in the rmail buffer.
  214.              ;; This way we make it available for the body
  215.              ;; even tho the rmail buffer is not current.
  216.              (let ((rmail-total-messages total))
  217.                (,@ body)))
  218.          (select-window window))))
  219.      (save-excursion
  220.        (set-buffer rmail-summary-buffer)
  221.        (let ((rmail-total-messages total))
  222.          (,@ body))))
  223.        (rmail-maybe-display-summary))))
  224.  
  225. ;;;; *** Rmail Mode ***
  226.  
  227. ;;;###autoload
  228. (defun rmail (&optional file-name-arg)
  229.   "Read and edit incoming mail.
  230. Moves messages into file named by `rmail-file-name' (a babyl format file)
  231.  and edits that file in RMAIL Mode.
  232. Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
  233.  
  234. May be called with file name as argument; then performs rmail editing on
  235. that file, but does not copy any new mail into the file.
  236. Interactively, if you supply a prefix argument, then you
  237. have a chance to specify a file name with the minibuffer.
  238.  
  239. If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
  240.   (interactive (if current-prefix-arg
  241.            (list (read-file-name "Run rmail on RMAIL file: "
  242.                      nil nil t))))
  243.   (or rmail-default-file
  244.       (setq rmail-default-file (expand-file-name "~/xmail")))
  245.   (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
  246.      (existed (get-file-buffer file-name)))
  247.     ;; Like find-file, but in the case where a buffer existed
  248.     ;; and the file was reverted, recompute the message-data.
  249.     (if (and existed (not (verify-visited-file-modtime existed)))
  250.     (progn
  251.       ;; Don't be confused by apparent local-variables spec
  252.       ;; in the last message in the RMAIL file.
  253.       (let ((enable-local-variables nil))
  254.         (find-file file-name))
  255.       (if (and (verify-visited-file-modtime existed)
  256.            (eq major-mode 'rmail-mode))
  257.           (progn (rmail-forget-messages)
  258.              (rmail-set-message-counters))))
  259.       (let ((enable-local-variables nil))
  260.     (find-file file-name)))
  261.     (if (eq major-mode 'rmail-edit-mode)
  262.     (error "Exit Rmail Edit mode before getting new mail."))
  263.     (if (and existed (> (buffer-size) 0))
  264.     ;; Buffer not new and not empty; ensure in proper mode, but that's all.
  265.     (or (eq major-mode 'rmail-mode)
  266.         (rmail-mode-2))
  267.       (rmail-mode-2)
  268.       ;; Convert all or part to Babyl file if possible.
  269.       (rmail-convert-file)
  270.       (goto-char (point-max))
  271.       (if (null rmail-inbox-list)
  272.       (progn
  273.         (rmail-set-message-counters)
  274.         (rmail-show-message))))
  275.     (let ((existing-unseen (rmail-first-unseen-message)))
  276.       (or file-name-arg
  277.       (rmail-get-new-mail))
  278.       ;; Show the first unseen message, which might be from a previous session
  279.       ;; or might have been just read in by rmail-get-new-mail.  Must
  280.       ;; determine already unseen messages first, as rmail-get-new-mail
  281.       ;; positions on the first new message, thus marking it as seen.
  282.       (rmail-show-message existing-unseen))
  283.     (if rmail-display-summary (rmail-summary))))
  284.  
  285. ;; Given the value of MAILPATH, return a list of inbox file names.
  286. ;; This is turned off because it is not clear that the user wants
  287. ;; all these inboxes to feed into the primary rmail file.
  288. ; (defun rmail-convert-mailpath (string)
  289. ;   (let (idx list)
  290. ;     (while (setq idx (string-match "[%:]" string))
  291. ;       (let ((this (substring string 0 idx)))
  292. ;     (setq string (substring string (1+ idx)))
  293. ;     (setq list (cons (if (string-match "%" this)
  294. ;                  (substring this 0 (string-match "%" this))
  295. ;                this)
  296. ;              list))))
  297. ;     list))
  298.  
  299. ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
  300. ; will not cause emacs 18.55 problems.
  301.  
  302. (defun rmail-convert-file ()
  303.   (let (convert)
  304.     (widen)
  305.     (goto-char (point-min))
  306.     ;; If file doesn't start like a Babyl file,
  307.     ;; convert it to one, by adding a header and converting each message.
  308.     (cond ((looking-at "BABYL OPTIONS:"))
  309.       ((looking-at "Version: 5\n")
  310.        ;; Losing babyl file made by old version of Rmail.
  311.        ;; Just fix the babyl file header; don't make a new one,
  312.        ;; so we don't lose the Labels: file attribute, etc.
  313.        (let ((buffer-read-only nil))
  314.          (insert "BABYL OPTIONS: -*- rmail -*-\n")))
  315.       ((equal (point-min) (point-max))
  316.        ;; Empty RMAIL file.  Just insert the header.
  317.        (rmail-insert-rmail-file-header))
  318.       (t
  319.        ;; Non-empty file in non-RMAIL format.  Add header and convert.
  320.        (setq convert t)
  321.        (rmail-insert-rmail-file-header)))
  322.     ;; If file was not a Babyl file or if there are
  323.     ;; Unix format messages added at the end,
  324.     ;; convert file as necessary.
  325.     (if (or convert
  326.         (save-excursion
  327.           (goto-char (point-max))
  328.           (search-backward "\^_")
  329.           (forward-char 1)
  330.           (looking-at "\n*From ")))
  331.     (let ((buffer-read-only nil))
  332.       (message "Converting to Babyl format...")
  333.       ;; If file needs conversion, convert it all,
  334.       ;; except for the BABYL header.
  335.       ;; (rmail-convert-to-babyl-format would delete the header.)
  336.       (goto-char (point-min))
  337.       (search-forward "\n\^_" nil t)
  338.       (narrow-to-region (point) (point-max))
  339.       (rmail-convert-to-babyl-format)
  340.       (message "Converting to Babyl format...done")))))
  341.  
  342. ;;; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
  343. ;;; will not cause emacs 18.55 problems.
  344.  
  345. (defun rmail-insert-rmail-file-header ()
  346.   (let ((buffer-read-only nil))
  347.     (insert "BABYL OPTIONS: -*- rmail -*-
  348. Version: 5
  349. Labels:
  350. Note:   This is the header of an rmail file.
  351. Note:   If you are seeing it in rmail,
  352. Note:    it means the file has no messages in it.\n\^_")))
  353.  
  354. (if rmail-mode-map
  355.     nil
  356.   (setq rmail-mode-map (make-keymap))
  357.   (suppress-keymap rmail-mode-map)
  358.   (define-key rmail-mode-map "a"      'rmail-add-label)
  359.   (define-key rmail-mode-map "b"      'rmail-bury)
  360.   (define-key rmail-mode-map "c"      'rmail-continue)
  361.   (define-key rmail-mode-map "d"      'rmail-delete-forward)
  362.   (define-key rmail-mode-map "\C-d"   'rmail-delete-backward)
  363.   (define-key rmail-mode-map "e"      'rmail-edit-current-message)
  364.   (define-key rmail-mode-map "f"      'rmail-forward)
  365.   (define-key rmail-mode-map "g"      'rmail-get-new-mail)
  366.   (define-key rmail-mode-map "h"      'rmail-summary)
  367.   (define-key rmail-mode-map "i"      'rmail-input)
  368.   (define-key rmail-mode-map "j"      'rmail-show-message)
  369.   (define-key rmail-mode-map "k"      'rmail-kill-label)
  370.   (define-key rmail-mode-map "l"      'rmail-summary-by-labels)
  371.   (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
  372.   (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
  373.   (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
  374.   (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
  375.   (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
  376.   (define-key rmail-mode-map "m"      'rmail-mail)
  377.   (define-key rmail-mode-map "\em"    'rmail-retry-failure)
  378.   (define-key rmail-mode-map "n"      'rmail-next-undeleted-message)
  379.   (define-key rmail-mode-map "\en"    'rmail-next-message)
  380.   (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
  381.   (define-key rmail-mode-map "o"      'rmail-output-to-rmail-file)
  382.   (define-key rmail-mode-map "\C-o"   'rmail-output)
  383.   (define-key rmail-mode-map "p"      'rmail-previous-undeleted-message)
  384.   (define-key rmail-mode-map "\ep"    'rmail-previous-message)
  385.   (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
  386.   (define-key rmail-mode-map "q"      'rmail-quit)
  387.   (define-key rmail-mode-map "r"      'rmail-reply)
  388. ;; I find I can't live without the default M-r command -- rms.
  389. ;;  (define-key rmail-mode-map "\er"  'rmail-search-backwards)
  390.   (define-key rmail-mode-map "s"      'rmail-expunge-and-save)
  391.   (define-key rmail-mode-map "\es"    'rmail-search)
  392.   (define-key rmail-mode-map "t"      'rmail-toggle-header)
  393.   (define-key rmail-mode-map "u"      'rmail-undelete-previous-message)
  394.   (define-key rmail-mode-map "w"      'rmail-edit-current-message)
  395.   (define-key rmail-mode-map "x"      'rmail-expunge)
  396.   (define-key rmail-mode-map "."      'rmail-beginning-of-message)
  397.   (define-key rmail-mode-map "<"      'rmail-first-message)
  398.   (define-key rmail-mode-map ">"      'rmail-last-message)
  399.   (define-key rmail-mode-map " "      'scroll-up)
  400.   (define-key rmail-mode-map "\177"   'scroll-down)
  401.   (define-key rmail-mode-map "?"      'describe-mode)
  402.   (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
  403.   (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
  404.   (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
  405.   (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
  406.   (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
  407.   (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
  408.   (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-keywords)
  409.   )
  410.  
  411. (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
  412.  
  413. (define-key rmail-mode-map [menu-bar classify]
  414.   (cons "Classify" (make-sparse-keymap "Classify")))
  415.  
  416. (define-key rmail-mode-map [menu-bar classify input-menu]
  417.   '("Input Rmail file (menu)..." . rmail-input-menu))
  418.  
  419. (define-key rmail-mode-map [menu-bar classify output-menu]
  420.   '("Output (Rmail menu)..." . rmail-output-menu))
  421.  
  422. (define-key rmail-mode-map [menu-bar classify output-inbox]
  423.   '("Output (inbox)..." . rmail-output))
  424.  
  425. (define-key rmail-mode-map [menu-bar classify output]
  426.   '("Output (Rmail)..." . rmail-output-to-rmail-file))
  427.  
  428. (define-key rmail-mode-map [menu-bar classify kill-label]
  429.   '("Kill Label..." . rmail-kill-label))
  430.  
  431. (define-key rmail-mode-map [menu-bar classify add-label]
  432.   '("Add Label..." . rmail-add-label))
  433.  
  434. (define-key rmail-mode-map [menu-bar summary]
  435.   (cons "Summary" (make-sparse-keymap "Summary")))
  436.  
  437. (define-key rmail-mode-map [menu-bar summary labels]
  438.   '("By Labels..." . rmail-summary-by-labels))
  439.  
  440. (define-key rmail-mode-map [menu-bar summary recipients]
  441.   '("By Recipients..." . rmail-summary-by-recipients))
  442.  
  443. (define-key rmail-mode-map [menu-bar summary topic]
  444.   '("By Topic..." . rmail-summary-by-topic))
  445.  
  446. (define-key rmail-mode-map [menu-bar summary regexp]
  447.   '("By Regexp..." . rmail-summary-by-regexp))
  448.  
  449. (define-key rmail-mode-map [menu-bar summary all]
  450.   '("All" . rmail-summary))
  451.  
  452. (define-key rmail-mode-map [menu-bar mail]
  453.   (cons "Mail" (make-sparse-keymap "Mail")))
  454.  
  455. (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
  456.   '("Get New Mail" . rmail-get-new-mail))
  457.  
  458. (define-key rmail-mode-map [menu-bar mail lambda]
  459.   '("----"))
  460.  
  461. (define-key rmail-mode-map [menu-bar mail continue]
  462.   '("Continue" . rmail-continue))
  463.  
  464. (define-key rmail-mode-map [menu-bar mail resend]
  465.   '("Re-send..." . rmail-resend))
  466.  
  467. (define-key rmail-mode-map [menu-bar mail forward]
  468.   '("Forward" . rmail-forward))
  469.  
  470. (define-key rmail-mode-map [menu-bar mail retry]
  471.   '("Retry" . rmail-retry-failure))
  472.  
  473. (define-key rmail-mode-map [menu-bar mail reply]
  474.   '("Reply" . rmail-reply))
  475.  
  476. (define-key rmail-mode-map [menu-bar mail mail]
  477.   '("Mail" . rmail-mail))
  478.  
  479. (define-key rmail-mode-map [menu-bar delete]
  480.   (cons "Delete" (make-sparse-keymap "Delete")))
  481.  
  482. (define-key rmail-mode-map [menu-bar delete expunge/save]
  483.   '("Expunge/Save" . rmail-expunge-and-save))
  484.  
  485. (define-key rmail-mode-map [menu-bar delete expunge]
  486.   '("Expunge" . rmail-expunge))
  487.  
  488. (define-key rmail-mode-map [menu-bar delete undelete]
  489.   '("Undelete" . rmail-undelete-previous-message))
  490.  
  491. (define-key rmail-mode-map [menu-bar delete delete]
  492.   '("Delete" . rmail-delete-forward))
  493.  
  494. (define-key rmail-mode-map [menu-bar move]
  495.   (cons "Move" (make-sparse-keymap "Move")))
  496.  
  497. (define-key rmail-mode-map [menu-bar move search-back]
  498.   '("Search Back..." . rmail-search-backward))
  499.  
  500. (define-key rmail-mode-map [menu-bar move search]
  501.   '("Search..." . rmail-search))
  502.  
  503. (define-key rmail-mode-map [menu-bar move previous]
  504.   '("Previous Nondeleted" . rmail-previous-undeleted-message))
  505.  
  506. (define-key rmail-mode-map [menu-bar move next]
  507.   '("Next Nondeleted" . rmail-next-undeleted-message))
  508.  
  509. (define-key rmail-mode-map [menu-bar move last]
  510.   '("Last" . rmail-last-message))
  511.  
  512. (define-key rmail-mode-map [menu-bar move first]
  513.   '("First" . rmail-first-message))
  514.  
  515. (define-key rmail-mode-map [menu-bar move previous]
  516.   '("Previous" . rmail-previous-message))
  517.  
  518. (define-key rmail-mode-map [menu-bar move next]
  519.   '("Next" . rmail-next-message))
  520.  
  521. ;; Rmail mode is suitable only for specially formatted data.
  522. (put 'rmail-mode 'mode-class 'special)
  523.  
  524. ;;;###autoload
  525. (defun rmail-mode ()
  526.   "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
  527. All normal editing commands are turned off.
  528. Instead, these commands are available:
  529.  
  530. \\[rmail-beginning-of-message]    Move point to front of this message (same as \\[beginning-of-buffer]).
  531. \\[scroll-up]    Scroll to next screen of this message.
  532. \\[scroll-down]    Scroll to previous screen of this message.
  533. \\[rmail-next-undeleted-message]    Move to Next non-deleted message.
  534. \\[rmail-previous-undeleted-message]    Move to Previous non-deleted message.
  535. \\[rmail-next-message]    Move to Next message whether deleted or not.
  536. \\[rmail-previous-message]    Move to Previous message whether deleted or not.
  537. \\[rmail-first-message]    Move to the first message in Rmail file.
  538. \\[rmail-last-message]    Move to the last message in Rmail file.
  539. \\[rmail-show-message]    Jump to message specified by numeric position in file.
  540. \\[rmail-search]    Search for string and show message it is found in.
  541. \\[rmail-delete-forward]    Delete this message, move to next nondeleted.
  542. \\[rmail-delete-backward]    Delete this message, move to previous nondeleted.
  543. \\[rmail-undelete-previous-message]    Undelete message.  Tries current message, then earlier messages
  544.     till a deleted message is found.
  545. \\[rmail-edit-current-message]    Edit the current message.  \\[rmail-cease-edit] to return to Rmail.
  546. \\[rmail-expunge]    Expunge deleted messages.
  547. \\[rmail-expunge-and-save]    Expunge and save the file.
  548. \\[rmail-quit]       Quit Rmail: expunge, save, then switch to another buffer.
  549. \\[save-buffer] Save without expunging.
  550. \\[rmail-get-new-mail]    Move new mail from system spool directory into this file.
  551. \\[rmail-mail]    Mail a message (same as \\[mail-other-window]).
  552. \\[rmail-continue]    Continue composing outgoing message started before.
  553. \\[rmail-reply]    Reply to this message.  Like \\[rmail-mail] but initializes some fields.
  554. \\[rmail-retry-failure]    Send this message again.  Used on a mailer failure message.
  555. \\[rmail-forward]    Forward this message to another user.
  556. \\[rmail-output-to-rmail-file]       Output this message to an Rmail file (append it).
  557. \\[rmail-output]    Output this message to a Unix-format mail file (append it).
  558. \\[rmail-input]    Input Rmail file.  Run Rmail on that file.
  559. \\[rmail-add-label]    Add label to message.  It will be displayed in the mode line.
  560. \\[rmail-kill-label]    Kill label.  Remove a label from current message.
  561. \\[rmail-next-labeled-message]   Move to Next message with specified label
  562.           (label defaults to last one specified).
  563.           Standard labels: filed, unseen, answered, forwarded, deleted.
  564.           Any other label is present only if you add it with \\[rmail-add-label].
  565. \\[rmail-previous-labeled-message]   Move to Previous message with specified label
  566. \\[rmail-summary]    Show headers buffer, with a one line summary of each message.
  567. \\[rmail-summary-by-labels]    Summarize only messages with particular label(s).
  568. \\[rmail-summary-by-recipients]   Summarize only messages with particular recipient(s).
  569. \\[rmail-summary-by-regexp]   Summarize only messages with particular regexp(s).
  570. \\[rmail-summary-by-topic]   Summarize only messages with subject line regexp(s).
  571. \\[rmail-toggle-header]    Toggle display of complete header."
  572.   (interactive)
  573.   (rmail-mode-2)
  574.   (rmail-set-message-counters)
  575.   (rmail-show-message rmail-total-messages))
  576.  
  577. (defun rmail-mode-2 ()
  578.   (kill-all-local-variables)
  579.   (rmail-mode-1)
  580.   (rmail-variables)
  581.   (run-hooks 'rmail-mode-hook))
  582.  
  583. (defun rmail-mode-1 ()
  584.   (setq major-mode 'rmail-mode)
  585.   (setq mode-name "RMAIL")
  586.   (setq buffer-read-only t)
  587.   ;; No need to auto save RMAIL files in normal circumstances
  588.   ;; because they contain no info except attribute changes
  589.   ;; and deletion of messages.
  590.   ;; The one exception is when messages are copied into an Rmail mode buffer.
  591.   ;; rmail-output-to-rmail-file enables auto save when you do that.
  592.   (setq buffer-auto-save-file-name nil)
  593.   (if (boundp 'mode-line-modified)
  594.       (setq mode-line-modified "--- ")
  595.     (setq mode-line-format
  596.       (cons "--- " (cdr (default-value 'mode-line-format)))))
  597.   (use-local-map rmail-mode-map)
  598.   (set-syntax-table text-mode-syntax-table)
  599.   (setq local-abbrev-table text-mode-abbrev-table))
  600.  
  601. (defun rmail-variables ()
  602.   (make-local-variable 'revert-buffer-function)
  603.   (setq revert-buffer-function 'rmail-revert)
  604.   (make-local-variable 'rmail-last-label)
  605.   (make-local-variable 'rmail-last-regexp)
  606.   (make-local-variable 'rmail-deleted-vector)
  607.   (make-local-variable 'rmail-summary-buffer)
  608.   (make-local-variable 'rmail-summary-vector)
  609.   (make-local-variable 'rmail-current-message)
  610.   (make-local-variable 'rmail-total-messages)
  611.   (make-local-variable 'require-final-newline)
  612.   (setq require-final-newline nil)
  613.   (make-local-variable 'rmail-overlay-list)
  614.   (setq rmail-overlay-list nil)
  615.   (make-local-variable 'version-control)
  616.   (setq version-control 'never)
  617.   (make-local-variable 'file-precious-flag)
  618.   (setq file-precious-flag t)
  619.   (make-local-variable 'rmail-message-vector)
  620.   (make-local-variable 'rmail-inbox-list)
  621.   (setq rmail-inbox-list (rmail-parse-file-inboxes))
  622.   ;; Provide default set of inboxes for primary mail file ~/RMAIL.
  623.   (and (null rmail-inbox-list)
  624.        (or (equal buffer-file-name (expand-file-name rmail-file-name))
  625.        (equal buffer-file-truename
  626.           (abbreviate-file-name (file-truename rmail-file-name))))
  627.        (setq rmail-inbox-list
  628.          (or rmail-primary-inbox-list
  629.          (list (or (getenv "MAIL")
  630.                (concat rmail-spool-directory
  631.                    (user-login-name)))))))
  632.   (make-local-variable 'rmail-keywords)
  633.   ;; this gets generated as needed
  634.   (setq rmail-keywords nil))
  635.  
  636. ;; Handle M-x revert-buffer done in an rmail-mode buffer.
  637. (defun rmail-revert (arg noconfirm)
  638.   (let (revert-buffer-function)
  639.     ;; Call our caller again, but this time it does the default thing.
  640.     (if (revert-buffer arg noconfirm)
  641.     ;; If the user said "yes", and we changed something,
  642.     ;; reparse the messages.
  643.     (progn
  644.       (rmail-convert-file)
  645.       (goto-char (point-max))
  646.       (rmail-set-message-counters)
  647.       (rmail-show-message)))))
  648.  
  649. ;; Return a list of files from this buffer's Mail: option.
  650. ;; Does not assume that messages have been parsed.
  651. ;; Just returns nil if buffer does not look like Babyl format.
  652. (defun rmail-parse-file-inboxes ()
  653.   (save-excursion
  654.     (save-restriction
  655.       (widen)
  656.       (goto-char 1)
  657.       (cond ((looking-at "BABYL OPTIONS:")
  658.          (search-forward "\n\^_" nil 'move)
  659.          (narrow-to-region 1 (point))
  660.          (goto-char 1)
  661.          (if (search-forward "\nMail:" nil t)
  662.          (progn
  663.            (narrow-to-region (point) (progn (end-of-line) (point)))
  664.            (goto-char (point-min))
  665.            (mail-parse-comma-list))))))))
  666.  
  667. (defun rmail-expunge-and-save ()
  668.   "Expunge and save RMAIL file."
  669.   (interactive)
  670.   (rmail-expunge)
  671.   (save-buffer)
  672.   (if (rmail-summary-exists)
  673.       (rmail-select-summary (set-buffer-modified-p nil))))
  674.  
  675. (defun rmail-quit ()
  676.   "Quit out of RMAIL."
  677.   (interactive)
  678.   (rmail-expunge-and-save)
  679.   ;; Don't switch to the summary buffer even if it was recently visible.
  680.   (if rmail-summary-buffer
  681.       (progn
  682.     (replace-buffer-in-windows rmail-summary-buffer)
  683.     (bury-buffer rmail-summary-buffer)))
  684.   (let ((obuf (current-buffer)))
  685.     (replace-buffer-in-windows obuf)
  686.     (bury-buffer obuf)))
  687.  
  688. ;;;###autoload
  689. (defun rmail-input (filename)
  690.   "Run Rmail on file FILENAME."
  691.   (interactive "FRun rmail on RMAIL file: ")
  692.   (rmail filename))
  693.  
  694. ;; Choose a .xmail file in dir rmail-secondary-file-directory.
  695. (defun rmail-secondary-file-menu (event)
  696.   (let ((files (directory-files rmail-secondary-file-directory nil
  697.                 rmail-secondary-file-regexp)))
  698.     (if files
  699.     (let* ((menu (list "Rmail Files"
  700.                (cons "Rmail Files"
  701.                  (mapcar (function (lambda (f) (cons f f)))
  702.                      files))))
  703.            (chosen (x-popup-menu event menu)))
  704.       (if chosen
  705.           (expand-file-name chosen rmail-secondary-file-directory)))
  706.       (message "No files matching %s%s found"
  707.            rmail-secondary-file-directory rmail-secondary-file-regexp)
  708.       nil)))
  709.  
  710.  
  711. (defun rmail-input-menu (event)
  712.   "Choose a new Rmail file to edit, with a menu.
  713. The variables `rmail-secondary-file-directory' and
  714. `rmail-secondary-file-regexp' control which files are offered in the menu."
  715.   (interactive "e")
  716.   (let ((file-name (rmail-secondary-file-menu event)))
  717.     (if file-name
  718.     (rmail-input file-name))))
  719.  
  720. ;;;; *** Rmail input ***
  721.  
  722. ;; RLK feature not added in this version:
  723. ;; argument specifies inbox file or files in various ways.
  724.  
  725. (defun rmail-get-new-mail (&optional file-name)
  726.   "Move any new mail from this RMAIL file's inbox files.
  727. The inbox files can be specified with the file's Mail: option.  The
  728. variable `rmail-primary-inbox-list' specifies the inboxes for your
  729. primary RMAIL file if it has no Mail: option.  By default, this is
  730. your /usr/spool/mail/$USER.
  731.  
  732. You can also specify the file to get new mail from.  In this case, the
  733. file of new mail is not changed or deleted.  Noninteractively, you can
  734. pass the inbox file name as an argument.  Interactively, a prefix
  735. argument causes us to read a file name and use that file as the inbox.
  736.  
  737. This function runs `rmail-get-new-mail-hook' before saving the updated file."
  738.   (interactive
  739.    (list (if current-prefix-arg
  740.          (read-file-name "Get new mail from file: "))))
  741.   (or (verify-visited-file-modtime (current-buffer))
  742.       (progn
  743.     (find-file (buffer-file-name))
  744.     (setq buffer-read-only t)
  745.     (if (verify-visited-file-modtime (current-buffer))
  746.         (rmail-forget-messages))))
  747.   (rmail-maybe-set-message-counters)
  748.   (widen)
  749.   ;; Get rid of all undo records for this buffer.
  750.   (or (eq buffer-undo-list t)
  751.       (setq buffer-undo-list nil))
  752.   (unwind-protect
  753.       (let ((opoint (point))
  754.         (new-messages 0)
  755.         (delete-files ())
  756.         ;; If buffer has not changed yet, and has not been saved yet,
  757.         ;; don't replace the old backup file now.
  758.         (make-backup-files (and make-backup-files (buffer-modified-p)))
  759.         (buffer-read-only nil)
  760.         ;; Don't make undo records for what we do in getting mail.
  761.         (buffer-undo-list t))
  762.     (goto-char (point-max))
  763.     (skip-chars-backward " \t\n")        ; just in case of brain damage
  764.     (delete-region (point) (point-max)) ; caused by require-final-newline
  765.     (save-excursion
  766.       (save-restriction
  767.         (narrow-to-region (point) (point))
  768.         ;; Read in the contents of the inbox files,
  769.         ;; renaming them as necessary,
  770.         ;; and adding to the list of files to delete eventually.
  771.         (if file-name
  772.         (rmail-insert-inbox-text (list file-name) nil)
  773.           (setq delete-files (rmail-insert-inbox-text rmail-inbox-list t)))
  774.         ;; Scan the new text and convert each message to babyl format.
  775.         (goto-char (point-min))
  776.         (save-excursion
  777.           (setq new-messages (rmail-convert-to-babyl-format)))
  778.         (or (zerop new-messages)
  779.         (let (success)
  780.           (widen)
  781.           (search-backward "\n\^_" nil t)
  782.           (narrow-to-region (point) (point-max))
  783.           (goto-char (1+ (point-min)))
  784.           (rmail-count-new-messages)
  785.           (run-hooks 'rmail-get-new-mail-hook)
  786.           (save-buffer)))
  787.         ;; Delete the old files, now that babyl file is saved.
  788.         (while delete-files
  789.           (condition-case ()
  790.           ;; First, try deleting.
  791.           (condition-case ()
  792.               (delete-file (car delete-files))
  793.             (file-error
  794.              ;; If we can't delete it, truncate it.
  795.              (write-region (point) (point) (car delete-files))))
  796.         (file-error nil))
  797.           (setq delete-files (cdr delete-files)))))
  798.     (if (= new-messages 0)
  799.         (progn (goto-char opoint)
  800.            (if (or file-name rmail-inbox-list)
  801.                (message "(No new mail has arrived)")))
  802.       (if (rmail-summary-exists)
  803.           (rmail-select-summary
  804.         (rmail-update-summary)))
  805.       (message "%d new message%s read"
  806.            new-messages (if (= 1 new-messages) "" "s"))
  807.       (and (boundp 'display-time-string)
  808.            (stringp display-time-string)
  809.            (string-match " Mail" display-time-string)
  810.            (setq display-time-string
  811.              (concat
  812.               (substring display-time-string 0 (match-beginning 0))
  813.               (substring display-time-string (match-end 0))))
  814.            (force-mode-line-update 'all))))
  815.     ;; Don't leave the buffer screwed up if we get a disk-full error.
  816.     (rmail-show-message)))
  817.  
  818. (defun rmail-insert-inbox-text (files renamep)
  819.   (let (file tofile delete-files movemail popmail)
  820.     (while files
  821.       (setq file (file-truename
  822.           (expand-file-name (substitute-in-file-name (car files))))
  823.         ;;>> un*x specific <<
  824.         ;; The "+" used to be "~", which is an extremely poor choice;
  825.         ;; it might accidentally be deleted when space is low
  826.         ;; (as happened to me!).
  827.         tofile (concat file "+"))
  828.       ;; If getting from mail spool directory,
  829.       ;; use movemail to move rather than just renaming,
  830.       ;; so as to interlock with the mailer.
  831.       (setq movemail (string= (file-name-directory file)
  832.                   (file-truename rmail-spool-directory))
  833.         popmail (string-match "^po:" (file-name-nondirectory file)))
  834.       (if popmail (setq file (file-name-nondirectory file)
  835.             renamep t))
  836.       (if movemail
  837.       (progn
  838.         (setq tofile (expand-file-name
  839.                ;; Generate name to move to from inbox name,
  840.                ;; in case of multiple inboxes that need moving.
  841.                (concat ".newmail-" (file-name-nondirectory file))
  842.                ;; Use the directory of this rmail file
  843.                ;; because it's a nuisance to use the homedir
  844.                ;; if that is on a full disk and this rmail
  845.                ;; file isn't.
  846.                (file-name-directory
  847.                  (expand-file-name buffer-file-name))))
  848.         ;; On some systems, /usr/spool/mail/foo is a directory
  849.         ;; and the actual inbox is /usr/spool/mail/foo/foo.
  850.         (if (file-directory-p file)
  851.         (setq file (expand-file-name (user-login-name)
  852.                          file)))))
  853.       (if popmail
  854.       (message "Getting mail from post office ...")
  855.     (if (or (and (file-exists-p tofile)
  856.              (/= 0 (nth 7 (file-attributes tofile))))
  857.         (and (file-exists-p file)
  858.              (/= 0 (nth 7 (file-attributes file)))))
  859.         (message "Getting mail from %s..." file)))
  860.       ;; Set TOFILE if have not already done so, and
  861.       ;; rename or copy the file FILE to TOFILE if and as appropriate.
  862.       (cond ((not renamep)
  863.          (setq tofile file))
  864.         ((or (file-exists-p tofile) (and (not popmail)
  865.                          (not (file-exists-p file))))
  866.          nil)
  867.         ((and (not movemail) (not popmail))
  868.          ;; Try copying.  If that fails (perhaps no space),
  869.          ;; rename instead.
  870.          (condition-case nil
  871.          (copy-file file tofile nil)
  872.            (error
  873.         ;; Third arg is t so we can replace existing file TOFILE.
  874.         (rename-file file tofile t)))
  875.          ;; Make the real inbox file empty.
  876.          ;; Leaving it deleted could cause lossage
  877.          ;; because mailers often won't create the file.
  878.          (condition-case ()
  879.          (write-region (point) (point) file)
  880.            (file-error nil)))
  881.         (t
  882.          (let ((errors nil))
  883.            (unwind-protect
  884.            (save-excursion
  885.              (setq errors (generate-new-buffer " *rmail loss*"))
  886.              (buffer-disable-undo errors)
  887.              (call-process
  888.               (or rmail-movemail-program
  889.               (expand-file-name "movemail" exec-directory))
  890.               nil errors nil file tofile)
  891.              (if (not (buffer-modified-p errors))
  892.              ;; No output => movemail won
  893.              nil
  894.                (set-buffer errors)
  895.                (subst-char-in-region (point-min) (point-max)
  896.                          ?\n ?\  )
  897.                (goto-char (point-max))
  898.                (skip-chars-backward " \t")
  899.                (delete-region (point) (point-max))
  900.                (goto-char (point-min))
  901.                (if (looking-at "movemail: ")
  902.                (delete-region (point-min) (match-end 0)))
  903.                (beep t)
  904.                (message (concat "movemail: "
  905.                     (buffer-substring (point-min)
  906.                               (point-max))))
  907.                (sit-for 3)
  908.                nil))
  909.          (if errors (kill-buffer errors))))))
  910.       ;; At this point, TOFILE contains the name to read:
  911.       ;; Either the alternate name (if we renamed)
  912.       ;; or the actual inbox (if not renaming).
  913.       (if (file-exists-p tofile)
  914.       (let (size)
  915.         (goto-char (point-max))
  916.         (setq size (nth 1 (insert-file-contents tofile)))
  917.         (goto-char (point-max))
  918.         (or (= (preceding-char) ?\n)
  919.         (zerop size)
  920.         (insert ?\n))
  921.         (setq delete-files (cons tofile delete-files))))
  922.       (message "")
  923.       (setq files (cdr files)))
  924.     delete-files))
  925.  
  926. ;; the  rmail-break-forwarded-messages  feature is not implemented
  927. (defun rmail-convert-to-babyl-format ()
  928.   (let ((count 0) start
  929.     (case-fold-search nil)
  930.     (invalid-input-resync
  931.      (function (lambda ()
  932.              (message "Invalid Babyl format in inbox!")
  933.              (sit-for 1)
  934.              ;; Try to get back in sync with a real message.
  935.              (if (re-search-forward
  936.               (concat mmdf-delim1 "\\|^From") nil t)
  937.              (beginning-of-line)
  938.                (goto-char (point-max)))))))
  939.     (goto-char (point-min))
  940.     (save-restriction
  941.       (while (not (eobp))
  942.     (cond ((looking-at "BABYL OPTIONS:");Babyl header
  943.            (if (search-forward "\n\^_" nil t)
  944.            ;; If we find the proper terminator, delete through there.
  945.            (delete-region (point-min) (point))
  946.          (funcall invalid-input-resync)
  947.          (delete-region (point-min) (point))))
  948.           ;; Babyl format message
  949.           ((looking-at "\^L")
  950.            (or (search-forward "\n\^_" nil t)
  951.            (funcall invalid-input-resync))
  952.            (setq count (1+ count))
  953.            ;; Make sure there is no extra white space after the ^_
  954.            ;; at the end of the message.
  955.            ;; Narrowing will make sure that whatever follows the junk
  956.            ;; will be treated properly.
  957.            (delete-region (point)
  958.                   (save-excursion
  959.                 (skip-chars-forward " \t\n")
  960.                 (point)))
  961.            (narrow-to-region (point) (point-max)))
  962.           ;;*** MMDF format
  963.           ((let ((case-fold-search t))
  964.          (looking-at mmdf-delim1))
  965.            (let ((case-fold-search t))
  966.          (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
  967.          (setq start (point))
  968.          (re-search-forward mmdf-delim2 nil t)
  969.          (replace-match "\^_"))
  970.            (save-excursion
  971.          (save-restriction
  972.            (narrow-to-region start (1- (point)))
  973.            (goto-char (point-min))
  974.            (while (search-forward "\n\^_" nil t); single char "\^_"
  975.              (replace-match "\n^_")))); 2 chars: "^" and "_"
  976.            (narrow-to-region (point) (point-max))
  977.            (setq count (1+ count)))
  978.           ;;*** Mail format
  979.           ((looking-at "^From ")
  980.            (setq start (point))
  981.            (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
  982.            (rmail-nuke-pinhead-header)
  983.            ;; If this message has a Content-Length field,
  984.            ;; skip to the end of the contents.
  985.            (let* ((header-end (save-excursion
  986.                     (and (re-search-forward "\n\n" nil t)
  987.                      (1- (point)))))
  988.               (case-fold-search t)
  989.               (size
  990.                ;; Get the numeric value from the Content-Length field.
  991.                (save-excursion
  992.              ;; Back up to end of prev line,
  993.              ;; in case the Content-Length field comes first.
  994.              (forward-char -1)
  995.              (and (search-forward "\ncontent-length: "
  996.                           header-end t)
  997.                   (let ((beg (point))
  998.                     (eol (progn (end-of-line) (point))))
  999.                 (string-to-int (buffer-substring beg eol)))))))
  1000.          (and size
  1001.               (if (and (natnump size)
  1002.                    (<= (+ header-end size) (point-max))
  1003.                    ;; Make sure this would put us at a position
  1004.                    ;; that we could continue from.
  1005.                    (save-excursion
  1006.                  (goto-char (+ header-end size))
  1007.                  (skip-chars-forward "\n")
  1008.                  (or (eobp)
  1009.                      (and (looking-at "BABYL OPTIONS:")
  1010.                       (search-forward "\n\^_" nil t))
  1011.                      (and (looking-at "\^L")
  1012.                       (search-forward "\n\^_" nil t))
  1013.                      (let ((case-fold-search t))
  1014.                        (looking-at mmdf-delim1))
  1015.                      (looking-at "From "))))
  1016.               (goto-char (+ header-end size))
  1017.             (message "Ignoring invalid Content-Length field")
  1018.             (sit-for 1 0 t))))
  1019.  
  1020.            (if (re-search-forward
  1021.             (concat "^[\^_]?\\("
  1022.                 rmail-unix-mail-delimiter
  1023.                 "\\|"
  1024.                 mmdf-delim1 "\\|"
  1025.                 "^BABYL OPTIONS:\\|"
  1026.                 "\^L\n[01],\\)") nil t)
  1027.            (goto-char (match-beginning 1))
  1028.          (goto-char (point-max)))
  1029.            (setq count (1+ count))
  1030.            (save-excursion
  1031.          (save-restriction
  1032.            (narrow-to-region start (point))
  1033.            (goto-char (point-min))
  1034.            (while (search-forward "\n\^_" nil t); single char
  1035.              (replace-match "\n^_")))); 2 chars: "^" and "_"
  1036.            (insert ?\^_)
  1037.            (narrow-to-region (point) (point-max)))
  1038.           ;;
  1039.           ;; This kludge is because some versions of sendmail.el
  1040.           ;; insert an extra newline at the beginning that shouldn't
  1041.           ;; be there.  sendmail.el has been fixed, but old versions
  1042.           ;; may still be in use.  -- rms, 7 May 1993.
  1043.           ((eolp) (delete-char 1))
  1044.           (t (error "Cannot convert to babyl format")))))
  1045.     count))
  1046.  
  1047. ;; Delete the "From ..." line, creating various other headers with
  1048. ;; information from it if they don't already exist.  Now puts the
  1049. ;; original line into a mail-from: header line for debugging and for
  1050. ;; use by the rmail-output function.
  1051. (defun rmail-nuke-pinhead-header ()
  1052.   (save-excursion
  1053.     (save-restriction
  1054.       (let ((start (point))
  1055.           (end (progn
  1056.            (condition-case ()
  1057.                (search-forward "\n\n")
  1058.              (error
  1059.               (goto-char (point-max))
  1060.               (insert "\n\n")))
  1061.            (point)))
  1062.         has-from has-date)
  1063.     (narrow-to-region start end)
  1064.     (let ((case-fold-search t))
  1065.       (goto-char start)
  1066.       (setq has-from (search-forward "\nFrom:" nil t))
  1067.       (goto-char start)
  1068.       (setq has-date (and (search-forward "\nDate:" nil t) (point)))
  1069.       (goto-char start))
  1070.     (let ((case-fold-search nil))
  1071.       (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
  1072.           (replace-match
  1073.         (concat
  1074.           "Mail-from: \\&"
  1075.           ;; Keep and reformat the date if we don't
  1076.           ;;  have a Date: field.
  1077.           (if has-date
  1078.               ""
  1079.             (concat
  1080.              "Date: \\3, \\5 \\4 \\9 \\6 "
  1081.             
  1082.              ;; The timezone could be matched by group 7 or group 10.
  1083.              ;; If neither of them matched, assume EST, since only
  1084.              ;; Easterners would be so sloppy.
  1085.              ;; It's a shame the substitution can't use "\\10".
  1086.              (cond
  1087.               ((/= (match-beginning 7) (match-end 7)) "\\7")
  1088.               ((/= (match-beginning 10) (match-end 10))
  1089.                (buffer-substring (match-beginning 10)
  1090.                      (match-end 10)))
  1091.               (t "EST"))
  1092.              "\n"))
  1093.           ;; Keep and reformat the sender if we don't
  1094.           ;; have a From: field.
  1095.           (if has-from
  1096.               ""
  1097.             "From: \\1\n"))
  1098.         t)))))))
  1099.  
  1100. ;;;; *** Rmail Message Formatting and Header Manipulation ***
  1101.  
  1102. (defun rmail-reformat-message (beg end)
  1103.   (goto-char beg)
  1104.   (forward-line 1)
  1105.   (if (/= (following-char) ?0)
  1106.       (error "Bad format in RMAIL file."))
  1107.   (let ((buffer-read-only nil)
  1108.     (delta (- (buffer-size) end)))
  1109.     (delete-char 1)
  1110.     (insert ?1)
  1111.     (forward-line 1)
  1112.     (let ((case-fold-search t))
  1113.       (while (looking-at "Summary-line:\\|Mail-From:")
  1114.      (forward-line 1)))
  1115.     (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
  1116.     (delete-region (point)
  1117.                (progn (forward-line 1) (point))))
  1118.     (let ((str (buffer-substring (point)
  1119.                  (save-excursion (search-forward "\n\n" end 'move)
  1120.                          (point)))))
  1121.       (insert str "*** EOOH ***\n")
  1122.       (narrow-to-region (point) (- (buffer-size) delta)))
  1123.     (goto-char (point-min))
  1124.     (if rmail-ignored-headers (rmail-clear-headers))
  1125.     (if rmail-message-filter (funcall rmail-message-filter))))
  1126.  
  1127. (defun rmail-clear-headers ()
  1128.   (if (search-forward "\n\n" nil t)
  1129.       (save-restriction
  1130.         (narrow-to-region (point-min) (point))
  1131.     (let ((buffer-read-only nil))
  1132.       (while (let ((case-fold-search t))
  1133.            (goto-char (point-min))
  1134.            (re-search-forward rmail-ignored-headers nil t))
  1135.         (beginning-of-line)
  1136.         (delete-region (point)
  1137.                (progn (re-search-forward "\n[^ \t]")
  1138.                   (forward-char -1)
  1139.                   (point))))))))
  1140.  
  1141. (defun rmail-toggle-header ()
  1142.   "Show original message header if pruned header currently shown, or vice versa."
  1143.   (interactive)
  1144.   (rmail-maybe-set-message-counters)
  1145.   (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
  1146.   (let ((buffer-read-only nil))
  1147.     (goto-char (point-min))
  1148.     (forward-line 1)
  1149.     (if (= (following-char) ?1)
  1150.     (progn (delete-char 1)
  1151.            (insert ?0)
  1152.            (forward-line 1)
  1153.            (let ((case-fold-search t))
  1154.           (while (looking-at "Summary-Line:\\|Mail-From:")
  1155.             (forward-line 1)))
  1156.            (insert "*** EOOH ***\n")
  1157.            (forward-char -1)
  1158.            (search-forward "\n*** EOOH ***\n")
  1159.            (forward-line -1)
  1160.            (let ((temp (point)))
  1161.          (and (search-forward "\n\n" nil t)
  1162.               (delete-region temp (point))))
  1163.            (goto-char (point-min))
  1164.            (search-forward "\n*** EOOH ***\n")
  1165.            (narrow-to-region (point) (point-max)))
  1166.       (rmail-reformat-message (point-min) (point-max))))
  1167.   (rmail-highlight-headers))
  1168.  
  1169. ;;;; *** Rmail Attributes and Keywords ***
  1170.  
  1171. ;; Make a string describing current message's attributes and keywords
  1172. ;; and set it up as the name of a minor mode
  1173. ;; so it will appear in the mode line.
  1174. (defun rmail-display-labels ()
  1175.   (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
  1176.     (save-excursion
  1177.       (unwind-protect
  1178.       (progn
  1179.         (widen)
  1180.         (goto-char (rmail-msgbeg rmail-current-message))
  1181.         (forward-line 1)
  1182.         (if (looking-at "[01],")
  1183.         (progn
  1184.           (narrow-to-region (point) (progn (end-of-line) (point)))
  1185.           ;; Truly valid BABYL format requires a space before each
  1186.           ;; attribute or keyword name.  Put them in if missing.
  1187.           (let (buffer-read-only)
  1188.             (goto-char (point-min))
  1189.             (while (search-forward "," nil t)
  1190.               (or (looking-at "[ ,]") (eobp)
  1191.               (insert " "))))
  1192.           (goto-char (point-max))
  1193.           (if (search-backward ",," nil 'move)
  1194.               (progn
  1195.             (if (> (point) (1+ (point-min)))
  1196.                 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
  1197.             (if (> (- (point-max) (point)) 2)
  1198.                 (setq blurb
  1199.                   (concat blurb
  1200.                       ";"
  1201.                       (buffer-substring (+ (point) 3)
  1202.                                 (1- (point-max)))))))))))
  1203.     ;; Note: we don't use save-restriction because that does not work right
  1204.     ;; if changes are made outside the saved restriction
  1205.     ;; before that restriction is restored.
  1206.     (narrow-to-region beg end)
  1207.     (set-marker beg nil)
  1208.     (set-marker end nil)))
  1209.     (while (string-match " +," blurb)
  1210.       (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
  1211.               (substring blurb (match-end 0)))))
  1212.     (while (string-match ", +" blurb)
  1213.       (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
  1214.               (substring blurb (match-end 0)))))
  1215.     (setq mode-line-process
  1216.       (concat " " rmail-current-message "/" rmail-total-messages
  1217.           blurb))))
  1218.  
  1219. ;; Turn an attribute of a message on or off according to STATE.
  1220. ;; ATTR is the name of the attribute, as a string.
  1221. ;; MSGNUM is message number to change; nil means current message.
  1222. (defun rmail-set-attribute (attr state &optional msgnum)
  1223.   (let ((omax (point-max-marker))
  1224.     (omin (point-min-marker))
  1225.     (buffer-read-only nil))
  1226.     (or msgnum (setq msgnum rmail-current-message))
  1227.     (if (> msgnum 0)
  1228.     (unwind-protect
  1229.         (save-excursion
  1230.           (widen)
  1231.           (goto-char (+ 3 (rmail-msgbeg msgnum)))
  1232.           (let ((curstate
  1233.              (not
  1234.               (null (search-backward (concat ", " attr ",")
  1235.                          (prog1 (point) (end-of-line)) t)))))
  1236.         (or (eq curstate (not (not state)))
  1237.             (if curstate
  1238.             (delete-region (point) (1- (match-end 0)))
  1239.               (beginning-of-line)
  1240.               (forward-char 2)
  1241.               (insert " " attr ","))))
  1242.           (if (string= attr "deleted")
  1243.           (rmail-set-message-deleted-p msgnum state)))
  1244.       ;; Note: we don't use save-restriction because that does not work right
  1245.       ;; if changes are made outside the saved restriction
  1246.       ;; before that restriction is restored.
  1247.       (narrow-to-region omin omax)
  1248.       (set-marker omin nil)
  1249.       (set-marker omax nil)
  1250.       (if (= msgnum rmail-current-message)
  1251.           (rmail-display-labels))))))
  1252.  
  1253. ;; Return t if the attributes/keywords line of msg number MSG
  1254. ;; contains a match for the regexp LABELS.
  1255. (defun rmail-message-labels-p (msg labels)
  1256.   (save-excursion
  1257.     (save-restriction
  1258.       (widen)
  1259.       (goto-char (rmail-msgbeg msg))
  1260.       (forward-char 3)
  1261.       (re-search-backward labels (prog1 (point) (end-of-line)) t))))
  1262.  
  1263. ;;;; *** Rmail Message Selection And Support ***
  1264.  
  1265. (defun rmail-msgend (n)
  1266.   (marker-position (aref rmail-message-vector (1+ n))))
  1267.  
  1268. (defun rmail-msgbeg (n)
  1269.   (marker-position (aref rmail-message-vector n)))
  1270.  
  1271. (defun rmail-widen-to-current-msgbeg (function)
  1272.   "Call FUNCTION with point at start of internal data of current message.
  1273. Assumes that bounds were previously narrowed to display the message in Rmail.
  1274. The bounds are widened enough to move point where desired, then narrowed
  1275. again afterward.
  1276.  
  1277. FUNCTION may not change the visible text of the message, but it may
  1278. change the invisible header text."
  1279.   (save-excursion
  1280.     (let ((obeg (- (point-max) (point-min))))
  1281.       (unwind-protect
  1282.       (progn
  1283.         (narrow-to-region (rmail-msgbeg rmail-current-message)
  1284.                   (point-max))
  1285.         (goto-char (point-min))
  1286.         (funcall function))
  1287.     ;; Note: we don't use save-restriction because that does not work right
  1288.     ;; if changes are made outside the saved restriction
  1289.     ;; before that restriction is restored.
  1290.     ;; Here we assume that changes made by FUNCTION
  1291.     ;; occur before the visible region of the message.
  1292.     (narrow-to-region (- (point-max) obeg) (point-max))))))
  1293.  
  1294. (defun rmail-forget-messages ()
  1295.   (unwind-protect
  1296.       (if (vectorp rmail-message-vector)
  1297.       (let* ((i 0)
  1298.          (v rmail-message-vector)
  1299.          (n (length v)))
  1300.         (while (< i n)
  1301.           (move-marker (aref v i)  nil)
  1302.           (setq i (1+ i)))))
  1303.     (setq rmail-message-vector nil)
  1304.     (setq rmail-deleted-vector nil)))
  1305.  
  1306. (defun rmail-maybe-set-message-counters ()
  1307.   (if (not (and rmail-deleted-vector
  1308.         rmail-message-vector
  1309.         rmail-current-message
  1310.         rmail-total-messages))
  1311.       (rmail-set-message-counters)))
  1312.  
  1313. (defun rmail-count-new-messages (&optional nomsg)
  1314.   (let* ((case-fold-search nil)
  1315.      (total-messages 0)
  1316.      (messages-head nil)
  1317.      (deleted-head nil))
  1318.     (or nomsg (message "Counting new messages..."))
  1319.     (goto-char (point-max))
  1320.     ;; Put at the end of messages-head
  1321.     ;; the entry for message N+1, which marks
  1322.     ;; the end of message N.  (N = number of messages).
  1323.     (search-backward "\n\^_")
  1324.     (forward-char 1)
  1325.     (setq messages-head (list (point-marker)))
  1326.     (rmail-set-message-counters-counter (point-min))
  1327.     (setq rmail-current-message (1+ rmail-total-messages))
  1328.     (setq rmail-total-messages
  1329.       (+ rmail-total-messages total-messages))
  1330.     (setq rmail-message-vector
  1331.       (vconcat rmail-message-vector (cdr messages-head)))
  1332.     (aset rmail-message-vector
  1333.       rmail-current-message (car messages-head))
  1334.     (setq rmail-deleted-vector
  1335.       (concat rmail-deleted-vector deleted-head))
  1336.     (setq rmail-summary-vector
  1337.       (vconcat rmail-summary-vector (make-vector total-messages nil)))
  1338.     (goto-char (point-min))
  1339.     (or nomsg (message "Counting new messages...done (%d)" total-messages))))
  1340.  
  1341. (defun rmail-set-message-counters ()
  1342.   (rmail-forget-messages)
  1343.   (save-excursion
  1344.     (save-restriction
  1345.       (widen)
  1346.       (let* ((point-save (point))
  1347.          (total-messages 0)
  1348.          (messages-after-point)
  1349.          (case-fold-search nil)
  1350.          (messages-head nil)
  1351.          (deleted-head nil))
  1352.     (message "Counting messages...")
  1353.     (goto-char (point-max))
  1354.     ;; Put at the end of messages-head
  1355.     ;; the entry for message N+1, which marks
  1356.     ;; the end of message N.  (N = number of messages).
  1357.     (search-backward "\n\^_" nil t)
  1358.     (if (/= (point) (point-max)) (forward-char 1))
  1359.     (setq messages-head (list (point-marker)))
  1360.     (rmail-set-message-counters-counter (min (point) point-save))
  1361.     (setq messages-after-point total-messages)
  1362.     (rmail-set-message-counters-counter)
  1363.     (setq rmail-total-messages total-messages)
  1364.     (setq rmail-current-message
  1365.           (min total-messages
  1366.            (max 1 (- total-messages messages-after-point))))
  1367.     (setq rmail-message-vector
  1368.           (apply 'vector (cons (point-min-marker) messages-head))
  1369.           rmail-deleted-vector (concat "D" deleted-head)
  1370.           rmail-summary-vector (make-vector rmail-total-messages nil))
  1371.     (message "Counting messages...done")))))
  1372.     
  1373. (defun rmail-set-message-counters-counter (&optional stop)
  1374.   (while (search-backward "\n\^_\^L\n" stop t)
  1375.     (forward-char 1)
  1376.     (setq messages-head (cons (point-marker) messages-head))
  1377.     (save-excursion
  1378.       (setq deleted-head
  1379.         (cons (if (search-backward ", deleted,"
  1380.                        (prog1 (point)
  1381.                      (forward-line 2))
  1382.                        t)
  1383.               ?D ?\ )
  1384.           deleted-head)))
  1385.     (if (zerop (% (setq total-messages (1+ total-messages)) 20))
  1386.     (message "Counting messages...%d" total-messages))))
  1387.  
  1388. (defun rmail-beginning-of-message ()
  1389.   "Show current message starting from the beginning."
  1390.   (interactive)
  1391.   (rmail-show-message rmail-current-message))
  1392.  
  1393. (defun rmail-show-message (&optional n)
  1394.   "Show message number N (prefix argument), counting from start of file.
  1395. If summary buffer is currently displayed, update current message there also."
  1396.   (interactive "p")
  1397.   (rmail-maybe-set-message-counters)
  1398.   (widen)
  1399.   (if (zerop rmail-total-messages)
  1400.       (progn (narrow-to-region (point-min) (1- (point-max)))
  1401.          (goto-char (point-min))
  1402.          (setq mode-line-process nil))
  1403.     (let (blurb)
  1404.       (if (not n)
  1405.       (setq n rmail-current-message)
  1406.     (cond ((<= n 0)
  1407.            (setq n 1
  1408.              rmail-current-message 1
  1409.              blurb "No previous message"))
  1410.           ((> n rmail-total-messages)
  1411.            (setq n rmail-total-messages
  1412.              rmail-current-message rmail-total-messages
  1413.              blurb "No following message"))
  1414.           (t
  1415.            (setq rmail-current-message n))))
  1416.       (let ((beg (rmail-msgbeg n))
  1417.         (end (rmail-msgend n)))
  1418.     (goto-char beg)
  1419.     (forward-line 1)
  1420.     (if (= (following-char) ?0)
  1421.         (progn
  1422.           (rmail-reformat-message beg end)
  1423.           (rmail-set-attribute "unseen" nil))
  1424.       (search-forward "\n*** EOOH ***\n" end t)
  1425.       (narrow-to-region (point) end))
  1426.     (goto-char (point-min))
  1427.     (rmail-display-labels)
  1428.     (rmail-highlight-headers)
  1429.     (if transient-mark-mode (deactivate-mark))
  1430.     (run-hooks 'rmail-show-message-hook)
  1431.     ;; If there is a summary buffer, try to move to this message
  1432.     ;; in that buffer.  But don't complain if this message
  1433.     ;; is not mentioned in the summary.
  1434.     (if (rmail-summary-exists)
  1435.         (let ((curr-msg rmail-current-message))
  1436.           (rmail-select-summary
  1437.            (rmail-summary-goto-msg curr-msg t t))))
  1438.     (if blurb
  1439.         (message blurb))))))
  1440.  
  1441. ;; Find all occurrences of certain fields, and highlight them.
  1442. (defun rmail-highlight-headers ()
  1443.   ;; Do this only if the system supports faces.
  1444.   (if (and (fboundp 'internal-find-face)
  1445.        rmail-highlighted-headers)
  1446.       (save-excursion
  1447.     (search-forward "\n\n" nil 'move)
  1448.     (save-restriction
  1449.       (narrow-to-region (point-min) (point))
  1450.       (let ((case-fold-search t)
  1451.         (inhibit-read-only t)
  1452.         ;; Highlight with boldface if that is available.
  1453.         ;; Otherwise use the `highlight' face.
  1454.         (face (or rmail-highlight-face
  1455.               (if (face-differs-from-default-p 'bold)
  1456.                   'bold 'highlight)))
  1457.         ;; List of overlays to reuse.
  1458.         (overlays rmail-overlay-list))
  1459.         (goto-char (point-min))
  1460.         (while (re-search-forward rmail-highlighted-headers nil t)
  1461.           (skip-chars-forward " \t")
  1462.           (let ((beg (point))
  1463.             overlay)
  1464.         (while (progn (forward-line 1)
  1465.                   (looking-at "[ \t]")))
  1466.         ;; Back up over newline, then trailing spaces or tabs
  1467.         (forward-char -1)
  1468.         (while (member (preceding-char) '(?  ?\t))
  1469.           (forward-char -1))
  1470.         (if overlays
  1471.             ;; Reuse an overlay we already have.
  1472.             (progn
  1473.               (setq overlay (car overlays)
  1474.                 overlays (cdr overlays))
  1475.               (overlay-put overlay 'face face)
  1476.               (move-overlay overlay beg (point)))
  1477.           ;; Make a new overlay and add it to
  1478.           ;; rmail-overlay-list.
  1479.           (setq overlay (make-overlay beg (point)))
  1480.           (overlay-put overlay 'face face)
  1481.           (setq rmail-overlay-list
  1482.             (cons overlay rmail-overlay-list))))))))))
  1483.  
  1484. (defun rmail-next-message (n)
  1485.   "Show following message whether deleted or not.
  1486. With prefix arg N, moves forward N messages, or backward if N is negative."
  1487.   (interactive "p")
  1488.   (rmail-maybe-set-message-counters)
  1489.   (rmail-show-message (+ rmail-current-message n)))
  1490.  
  1491. (defun rmail-previous-message (n)
  1492.   "Show previous message whether deleted or not.
  1493. With prefix arg N, moves backward N messages, or forward if N is negative."
  1494.   (interactive "p")
  1495.   (rmail-next-message (- n)))  
  1496.  
  1497. (defun rmail-next-undeleted-message (n)
  1498.   "Show following non-deleted message.
  1499. With prefix arg N, moves forward N non-deleted messages,
  1500. or backward if N is negative.
  1501.  
  1502. Returns t if a new message is being shown, nil otherwise."
  1503.   (interactive "p")
  1504.   (rmail-maybe-set-message-counters)
  1505.   (let ((lastwin rmail-current-message)
  1506.     (current rmail-current-message))
  1507.     (while (and (> n 0) (< current rmail-total-messages))
  1508.       (setq current (1+ current))
  1509.       (if (not (rmail-message-deleted-p current))
  1510.       (setq lastwin current n (1- n))))
  1511.     (while (and (< n 0) (> current 1))
  1512.       (setq current (1- current))
  1513.       (if (not (rmail-message-deleted-p current))
  1514.       (setq lastwin current n (1+ n))))
  1515.     (if (/= lastwin rmail-current-message)
  1516.      (progn (rmail-show-message lastwin)
  1517.             t)
  1518.       (if (< n 0)
  1519.       (message "No previous nondeleted message"))
  1520.       (if (> n 0)
  1521.       (message "No following nondeleted message"))
  1522.       nil)))
  1523.  
  1524. (defun rmail-previous-undeleted-message (n)
  1525.   "Show previous non-deleted message.
  1526. With prefix argument N, moves backward N non-deleted messages,
  1527. or forward if N is negative."
  1528.   (interactive "p")
  1529.   (rmail-next-undeleted-message (- n)))
  1530.  
  1531. (defun rmail-first-message ()
  1532.   "Show first message in file."
  1533.   (interactive)
  1534.   (rmail-maybe-set-message-counters)
  1535.   (rmail-show-message 1))
  1536.  
  1537. (defun rmail-last-message ()
  1538.   "Show last message in file."
  1539.   (interactive)
  1540.   (rmail-maybe-set-message-counters)
  1541.   (rmail-show-message rmail-total-messages))
  1542.  
  1543. (defun rmail-what-message ()
  1544.   (let ((where (point))
  1545.     (low 1)
  1546.     (high rmail-total-messages)
  1547.     (mid (/ rmail-total-messages 2)))
  1548.     (while (> (- high low) 1)
  1549.       (if (>= where (rmail-msgbeg mid))
  1550.       (setq low mid)
  1551.     (setq high mid))
  1552.       (setq mid (+ low (/ (- high low) 2))))
  1553.     (if (>= where (rmail-msgbeg high)) high low)))
  1554.  
  1555. (defun rmail-message-recipients-p (msg recipients &optional primary-only)
  1556.   (save-restriction
  1557.     (goto-char (rmail-msgbeg msg))
  1558.     (search-forward "\n*** EOOH ***\n")
  1559.     (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
  1560.     (or (string-match recipients (or (mail-fetch-field "To") ""))
  1561.     (string-match recipients (or (mail-fetch-field "From") ""))
  1562.     (if (not primary-only)
  1563.         (string-match recipients (or (mail-fetch-field "Cc") ""))))))
  1564.  
  1565. (defun rmail-message-regexp-p (msg regexp)
  1566.   "Return t, if for message number MSG, regexp REGEXP matches in the header."
  1567.   (goto-char (rmail-msgbeg msg))
  1568.   (let ((end 
  1569.          (save-excursion 
  1570.            (search-forward "*** EOOH ***" (point-max)) (point))))
  1571.     (re-search-forward regexp end t)))
  1572.  
  1573. (defun rmail-search-backward (regexp &optional n)
  1574.   "Show message containing next match for REGEXP.
  1575. Prefix argument gives repeat count; negative argument means search
  1576. backwards (through earlier messages).
  1577. Interactively, empty argument means use same regexp used last time."
  1578.   (interactive
  1579.     (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
  1580.        (prompt
  1581.         (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1582.        regexp)
  1583.       (if rmail-search-last-regexp
  1584.       (setq prompt (concat prompt
  1585.                    "(default "
  1586.                    rmail-search-last-regexp
  1587.                    ") ")))
  1588.       (setq regexp (read-string prompt))
  1589.       (cond ((not (equal regexp ""))
  1590.          (setq rmail-search-last-regexp regexp))
  1591.         ((not rmail-search-last-regexp)
  1592.          (error "No previous Rmail search string")))
  1593.       (list rmail-search-last-regexp
  1594.         (prefix-numeric-value current-prefix-arg))))
  1595.   (rmail-search regexp (- n)))
  1596.  
  1597. (defvar rmail-search-last-regexp nil)
  1598. (defun rmail-search (regexp &optional n)
  1599.   "Show message containing next match for REGEXP.
  1600. Prefix argument gives repeat count; negative argument means search
  1601. backwards (through earlier messages).
  1602. Interactively, empty argument means use same regexp used last time."
  1603.   (interactive
  1604.     (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
  1605.        (prompt
  1606.         (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1607.        regexp)
  1608.       (if rmail-search-last-regexp
  1609.       (setq prompt (concat prompt
  1610.                    "(default "
  1611.                    rmail-search-last-regexp
  1612.                    ") ")))
  1613.       (setq regexp (read-string prompt))
  1614.       (cond ((not (equal regexp ""))
  1615.          (setq rmail-search-last-regexp regexp))
  1616.         ((not rmail-search-last-regexp)
  1617.          (error "No previous Rmail search string")))
  1618.       (list rmail-search-last-regexp
  1619.         (prefix-numeric-value current-prefix-arg))))
  1620.   (or n (setq n 1))
  1621.   (message "%sRmail search for %s..."
  1622.        (if (< n 0) "Reverse " "")
  1623.        regexp)
  1624.   (rmail-maybe-set-message-counters)
  1625.   (let ((omin (point-min))
  1626.     (omax (point-max))
  1627.     (opoint (point))
  1628.     win
  1629.     (reversep (< n 0))
  1630.     (msg rmail-current-message))
  1631.     (unwind-protect
  1632.     (progn
  1633.       (widen)
  1634.       (while (/= n 0)
  1635.         ;; Check messages one by one, advancing message number up or down
  1636.         ;; but searching forward through each message.
  1637.         (if reversep
  1638.         (while (and (null win) (> msg 1))
  1639.           (goto-char (rmail-msgbeg (setq msg (1- msg))))
  1640.           (setq win (re-search-forward
  1641.                  regexp (rmail-msgend msg) t)))
  1642.           (while (and (null win) (< msg rmail-total-messages))
  1643.         (goto-char (rmail-msgbeg (setq msg (1+ msg))))
  1644.         (setq win (re-search-forward regexp (rmail-msgend msg) t))))
  1645.         (setq n (+ n (if reversep 1 -1)))))
  1646.       (if win
  1647.       (progn
  1648.         ;; If this is a reverse search and we found a message,
  1649.         ;; search backward thru this message to position point.
  1650.         (if reversep
  1651.         (progn
  1652.           (goto-char (rmail-msgend msg))
  1653.           (re-search-backward
  1654.            regexp (rmail-msgbeg msg) t)))
  1655.         (setq win (point))
  1656.         (rmail-show-message msg)
  1657.         (message "%sRmail search for %s...done"
  1658.              (if reversep "Reverse " "")
  1659.              regexp)
  1660.         (goto-char win))
  1661.     (goto-char opoint)
  1662.     (narrow-to-region omin omax)
  1663.     (ding)
  1664.     (message "Search failed: %s" regexp)))))
  1665.  
  1666. (defun rmail-search-backwards (regexp &optional n)
  1667.   "Show message containing previous match for REGEXP.
  1668. Prefix argument gives repeat count; negative argument means search
  1669. forward (through later messages).
  1670. Interactively, empty argument means use same regexp used last time."
  1671.   (interactive
  1672.     (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
  1673.        (prompt
  1674.         (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1675.        regexp)
  1676.       (if rmail-search-last-regexp
  1677.       (setq prompt (concat prompt
  1678.                    "(default "
  1679.                    rmail-search-last-regexp
  1680.                    ") ")))
  1681.       (setq regexp (read-string prompt))
  1682.       (cond ((not (equal regexp ""))
  1683.          (setq rmail-search-last-regexp regexp))
  1684.         ((not rmail-search-last-regexp)
  1685.          (error "No previous Rmail search string")))
  1686.       (list rmail-search-last-regexp
  1687.         (prefix-numeric-value current-prefix-arg))))
  1688.   (rmail-search regexp (- (or n -1))))
  1689.  
  1690. ;; Show the first message which has the `unseen' attribute.
  1691. (defun rmail-first-unseen-message ()
  1692.   (rmail-maybe-set-message-counters)
  1693.   (let ((current 1)
  1694.     found)
  1695.     (save-restriction
  1696.       (widen)
  1697.       (while (and (not found) (< current rmail-total-messages))
  1698.     (if (rmail-message-labels-p current ", ?\\(unseen\\),")
  1699.         (setq found current))
  1700.     (setq current (1+ current))))
  1701. ;; Let the caller show the message.
  1702. ;;    (if found
  1703. ;;    (rmail-show-message found))
  1704.     found))
  1705.  
  1706. ;;;; *** Rmail Message Deletion Commands ***
  1707.  
  1708. (defun rmail-message-deleted-p (n)
  1709.   (= (aref rmail-deleted-vector n) ?D))
  1710.  
  1711. (defun rmail-set-message-deleted-p (n state)
  1712.   (aset rmail-deleted-vector n (if state ?D ?\ )))
  1713.  
  1714. (defun rmail-delete-message ()
  1715.   "Delete this message and stay on it."
  1716.   (interactive)
  1717.   (rmail-set-attribute "deleted" t))
  1718.  
  1719. (defun rmail-undelete-previous-message ()
  1720.   "Back up to deleted message, select it, and undelete it."
  1721.   (interactive)
  1722.   (let ((msg rmail-current-message))
  1723.     (while (and (> msg 0)
  1724.         (not (rmail-message-deleted-p msg)))
  1725.       (setq msg (1- msg)))
  1726.     (if (= msg 0)
  1727.     (error "No previous deleted message")
  1728.       (if (/= msg rmail-current-message)
  1729.       (rmail-show-message msg))
  1730.       (rmail-set-attribute "deleted" nil)
  1731.       (if (rmail-summary-exists)
  1732.       (save-excursion
  1733.         (set-buffer rmail-summary-buffer)
  1734.         (rmail-summary-mark-undeleted msg)))
  1735.       (rmail-maybe-display-summary))))
  1736.  
  1737. (defun rmail-delete-forward (&optional backward)
  1738.   "Delete this message and move to next nondeleted one.
  1739. Deleted messages stay in the file until the \\[rmail-expunge] command is given.
  1740. With prefix argument, delete and move backward.
  1741.  
  1742. Returns t if a new message is displayed after the delete, or nil otherwise."
  1743.   (interactive "P")
  1744.   (rmail-set-attribute "deleted" t)
  1745.   (let ((del-msg rmail-current-message))
  1746.     (if (rmail-summary-exists)
  1747.     (save-excursion
  1748.       (set-buffer rmail-summary-buffer)
  1749.       (rmail-summary-mark-deleted del-msg)))
  1750.     (prog1 (rmail-next-undeleted-message (if backward -1 1))
  1751.       (rmail-maybe-display-summary))))
  1752.  
  1753. (defun rmail-delete-backward ()
  1754.   "Delete this message and move to previous nondeleted one.
  1755. Deleted messages stay in the file until the \\[rmail-expunge] command is given."
  1756.   (interactive)
  1757.   (rmail-delete-forward t))
  1758.  
  1759. (defun rmail-only-expunge ()
  1760.   "Actually erase all deleted messages in the file."
  1761.   (interactive)
  1762.   (message "Expunging deleted messages...")
  1763.   ;; Discard all undo records for this buffer.
  1764.   (or (eq buffer-undo-list t)
  1765.       (setq buffer-undo-list nil))
  1766.   (rmail-maybe-set-message-counters)
  1767.   (let* ((omax (- (buffer-size) (point-max)))
  1768.      (omin (- (buffer-size) (point-min)))
  1769.      (opoint (if (and (> rmail-current-message 0)
  1770.               (rmail-message-deleted-p rmail-current-message))
  1771.              0
  1772.            (- (point) (point-min))))
  1773.      (messages-head (cons (aref rmail-message-vector 0) nil))
  1774.      (messages-tail messages-head)
  1775.      ;; Don't make any undo records for the expunging.
  1776.      (buffer-undo-list t)
  1777.      (win))
  1778.     (unwind-protect
  1779.     (save-excursion
  1780.       (widen)
  1781.       (goto-char (point-min))
  1782.       (let ((counter 0)
  1783.         (number 1)
  1784.         (total rmail-total-messages)
  1785.         (new-message-number rmail-current-message)
  1786.         (new-summary nil)
  1787.         (buffer-read-only nil)
  1788.         (messages rmail-message-vector)
  1789.         (deleted rmail-deleted-vector)
  1790.         (summary rmail-summary-vector))
  1791.         (setq rmail-total-messages nil
  1792.           rmail-current-message nil
  1793.           rmail-message-vector nil
  1794.           rmail-deleted-vector nil
  1795.           rmail-summary-vector nil)
  1796.         (while (<= number total)
  1797.           (if (= (aref deleted number) ?D)
  1798.           (progn
  1799.             (delete-region
  1800.               (marker-position (aref messages number))
  1801.               (marker-position (aref messages (1+ number))))
  1802.             (move-marker (aref messages number) nil)
  1803.             (if (> new-message-number counter)
  1804.             (setq new-message-number (1- new-message-number))))
  1805.         (setq counter (1+ counter))
  1806.         (setq messages-tail
  1807.               (setcdr messages-tail
  1808.                   (cons (aref messages number) nil)))
  1809.         (setq new-summary
  1810.               (cons (if (= counter number) (aref summary (1- number)))
  1811.                 new-summary)))
  1812.           (if (zerop (% (setq number (1+ number)) 20))
  1813.           (message "Expunging deleted messages...%d" number)))
  1814.         (setq messages-tail
  1815.           (setcdr messages-tail
  1816.               (cons (aref messages number) nil)))
  1817.         (setq rmail-current-message new-message-number
  1818.           rmail-total-messages counter
  1819.           rmail-message-vector (apply 'vector messages-head)
  1820.           rmail-deleted-vector (make-string (1+ counter) ?\ )
  1821.           rmail-summary-vector (vconcat (nreverse new-summary))
  1822.           win t)))
  1823.       (message "Expunging deleted messages...done")
  1824.       (if (not win)
  1825.       (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
  1826.       (rmail-show-message
  1827.        (if (zerop rmail-current-message) 1 nil))
  1828.       (forward-char opoint))))
  1829.  
  1830. (defun rmail-expunge ()
  1831.   "Erase deleted messages from Rmail file and summary buffer."
  1832.   (interactive)
  1833.   (rmail-only-expunge)
  1834.   (if (rmail-summary-exists)
  1835.       (rmail-select-summary
  1836.     (rmail-update-summary))))
  1837.  
  1838. ;;;; *** Rmail Mailing Commands ***
  1839.  
  1840. (defun rmail-start-mail (&rest args)
  1841.   (if (and window-system rmail-mail-new-frame)
  1842.       (prog1
  1843.     (apply 'mail-other-frame args)
  1844.     (modify-frame-parameters (selected-frame)
  1845.                  '((dedicated . t))))
  1846.     (apply 'mail-other-window args)))
  1847.  
  1848. (defun rmail-mail ()
  1849.   "Send mail in another window.
  1850. While composing the message, use \\[mail-yank-original] to yank the
  1851. original message into it."
  1852.   (interactive)
  1853.   (rmail-start-mail nil nil nil nil nil (current-buffer)))
  1854.  
  1855. (defun rmail-continue ()
  1856.   "Continue composing outgoing message previously being composed."
  1857.   (interactive)
  1858.   (rmail-start-mail t))
  1859.  
  1860. (defun rmail-reply (just-sender)
  1861.   "Reply to the current message.
  1862. Normally include CC: to all other recipients of original message;
  1863. prefix argument means ignore them.  While composing the reply,
  1864. use \\[mail-yank-original] to yank the original message into it."
  1865.   (interactive "P")
  1866.   (let (from reply-to cc subject date to message-id resent-reply-to)
  1867.     (save-excursion
  1868.       (save-restriction
  1869.     (widen)
  1870.     (goto-char (rmail-msgbeg rmail-current-message))
  1871.     (forward-line 1)
  1872.     (if (= (following-char) ?0)
  1873.         (narrow-to-region
  1874.          (progn (forward-line 2)
  1875.             (point))
  1876.          (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
  1877.                     'move)
  1878.             (point)))
  1879.       (narrow-to-region (point)
  1880.                 (progn (search-forward "\n*** EOOH ***\n")
  1881.                    (beginning-of-line) (point))))
  1882.     (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
  1883.           from (mail-fetch-field "from")
  1884.           reply-to (or resent-reply-to
  1885.                (mail-fetch-field "reply-to" nil t)
  1886.                from)
  1887.           cc (cond (just-sender nil)
  1888.                (resent-reply-to (mail-fetch-field "resent-cc" t))
  1889.                (t (mail-fetch-field "cc" nil t)))
  1890.           subject (or (and resent-reply-to
  1891.                    (mail-fetch-field "resent-subject" t))
  1892.               (mail-fetch-field "subject"))
  1893.           date (or (and resent-reply-to
  1894.                 (mail-fetch-field "resent-date" t))
  1895.                (mail-fetch-field "date"))
  1896.           to (cond (resent-reply-to
  1897.             (or (mail-fetch-field "resent-to" t)) "")
  1898.                ((mail-fetch-field "to" nil t))
  1899.                ;((mail-fetch-field "apparently-to")) ack gag barf
  1900.                (t ""))
  1901.           message-id (cond (resent-reply-to
  1902.                 (mail-fetch-field "resent-message-id" t))
  1903.                    ((mail-fetch-field "message-id"))))))
  1904.     (and (stringp subject)
  1905.      (or (string-match (concat "\\`" (regexp-quote rmail-reply-prefix))
  1906.                subject)
  1907.          (setq subject (concat rmail-reply-prefix subject))))
  1908.     (rmail-start-mail nil
  1909.       (mail-strip-quoted-names reply-to)
  1910.       subject
  1911.       (rmail-make-in-reply-to-field from date message-id)
  1912.       (if just-sender
  1913.       nil
  1914.     (let* ((cc-list (rmail-dont-reply-to
  1915.               (mail-strip-quoted-names
  1916.                 (if (null cc) to (concat to ", " cc))))))
  1917.       (if (string= cc-list "") nil cc-list)))
  1918.       (current-buffer)
  1919.       (list (list '(lambda (buf msgnum)
  1920.              (save-excursion
  1921.                (set-buffer buf)
  1922.                (rmail-set-attribute "answered" t msgnum)))
  1923.           (current-buffer) rmail-current-message)))))
  1924.  
  1925. (defun rmail-make-in-reply-to-field (from date message-id)
  1926.   (cond ((not from)
  1927.          (if message-id
  1928.              message-id
  1929.              nil))
  1930.         (mail-use-rfc822
  1931.          (require 'rfc822)
  1932.          (let ((tem (car (rfc822-addresses from))))
  1933.            (if message-id
  1934.                (if (string-match
  1935.                     (regexp-quote (if (string-match "@[^@]*\\'" tem)
  1936.                                       (substring tem 0 (match-beginning 0))
  1937.                                       tem))
  1938.                     message-id)
  1939.                    ;; Message-ID is sufficiently informative
  1940.                    message-id
  1941.                    (concat message-id " (" tem ")"))
  1942.          ;; Copy TEM, discarding text properties.
  1943.          (setq tem (copy-sequence tem))
  1944.          (set-text-properties 0 (length tem) nil tem)
  1945.          (setq tem (copy-sequence tem))
  1946.          ;; Use prin1 to fake RFC822 quoting
  1947.          (let ((field (prin1-to-string tem)))
  1948.            (if date
  1949.            (concat field "'s message of " date)
  1950.            field)))))
  1951.         ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
  1952.                 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
  1953.            ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
  1954.            (or (string-match (concat "\\`[ \t]*\\(" bar
  1955.                                      "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
  1956.                              ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
  1957.                              from)
  1958.                (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
  1959.                                      bar "\\))[ \t]*\\'")
  1960.                              ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
  1961.                              from)))
  1962.          (let ((start (match-beginning 1))
  1963.                (end (match-end 1)))
  1964.            ;; Trim whitespace which above regexp match allows
  1965.            (while (and (< start end)
  1966.                        (memq (aref from start) '(?\t ?\ )))
  1967.              (setq start (1+ start)))
  1968.            (while (and (< start end)
  1969.                        (memq (aref from (1- end)) '(?\t ?\ )))
  1970.              (setq end (1- end)))
  1971.            (let ((field (substring from start end)))
  1972.              (if date (setq field (concat "message from " field " on " date)))
  1973.              (if message-id
  1974.                  ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
  1975.                  (concat message-id " (" field ")")
  1976.                  field))))
  1977.         (t
  1978.          ;; If we can't kludge it simply, do it correctly
  1979.          (let ((mail-use-rfc822 t))
  1980.            (rmail-make-in-reply-to-field from date message-id)))))
  1981.  
  1982. (defun rmail-forward (resend)
  1983.   "Forward the current message to another user.
  1984. With prefix argument, \"resend\" the message instead of forwarding it;
  1985. see the documentation of `rmail-resend'."
  1986.   (interactive "P")
  1987.   (if resend
  1988.       (call-interactively 'rmail-resend)
  1989.     (let ((forward-buffer (current-buffer))
  1990.       (subject (concat "["
  1991.                (let ((from (or (mail-fetch-field "From")
  1992.                        (mail-fetch-field ">From"))))
  1993.                  (if from
  1994.                  (concat (mail-strip-quoted-names from) ": ")
  1995.                    ""))
  1996.                (or (mail-fetch-field "Subject") "")
  1997.                "]")))
  1998.       ;; If only one window, use it for the mail buffer.
  1999.       ;; Otherwise, use another window for the mail buffer
  2000.       ;; so that the Rmail buffer remains visible
  2001.       ;; and sending the mail will get back to it.
  2002.       (if (funcall (if (and (not rmail-mail-new-frame) (one-window-p t))
  2003.                (function mail)
  2004.              (function rmail-start-mail))
  2005.            nil nil subject nil nil nil
  2006.            (list (list (function (lambda (buf msgnum)
  2007.                        (save-excursion
  2008.                          (set-buffer buf)
  2009.                          (rmail-set-attribute
  2010.                           "forwarded" t msgnum))))
  2011.                    (current-buffer)
  2012.                    rmail-current-message)))
  2013.       (save-excursion
  2014.         ;; Insert after header separator--before signature if any.
  2015.         (goto-char (point-min))
  2016.         (search-forward-regexp
  2017.          (concat "^" (regexp-quote mail-header-separator) "$"))
  2018.         (forward-line 1)
  2019.         (insert-buffer forward-buffer))))))
  2020.  
  2021. (defun rmail-resend (address &optional from comment mail-alias-file)
  2022.   "Resend current message to ADDRESSES.
  2023. ADDRESSES should be a single address, a string consisting of several
  2024. addresses separated by commas, or a list of addresses.
  2025.  
  2026. Optional FROM is the address to resend the message from, and
  2027. defaults to the username of the person redistributing the message.
  2028. Optional COMMENT is a string that will be inserted as a comment in the
  2029. resent message.
  2030. Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
  2031. typically for purposes of moderating a list."
  2032.   (interactive "sResend to: ")
  2033.   (require 'sendmail)
  2034.   (require 'mailalias)
  2035.   (if (not from) (setq from (user-login-name)))
  2036.   (let ((tembuf (generate-new-buffer " sendmail temp"))
  2037.     (mail-header-separator "")
  2038.     (case-fold-search nil)
  2039.     (mailbuf (current-buffer)))
  2040.     (unwind-protect
  2041.     (save-excursion
  2042.       ;;>> Copy message into temp buffer
  2043.       (set-buffer tembuf)
  2044.       (insert-buffer-substring mailbuf)
  2045.       (goto-char (point-min))
  2046.       ;; Delete any Sender field, since that's not specifyable.
  2047.       ; Only delete Sender fields in the actual header.
  2048.       (re-search-forward "^$" nil 'move)
  2049.       ; Using "while" here rather than "if" because some buggy mail
  2050.       ; software may have inserted multiple Sender fields.
  2051.       (while (re-search-backward "^Sender:" nil t)
  2052.         (let (beg)
  2053.           (setq beg (point))
  2054.           (forward-line 1)
  2055.           (while (looking-at "[ \t]")
  2056.         (forward-line 1))
  2057.           (delete-region beg (point))))
  2058.       ; Go back to the beginning of the buffer so the Resent- fields
  2059.       ; are inserted there.
  2060.       (goto-char (point-min))
  2061.       ;;>> Insert resent-from:
  2062.       (insert "Resent-From: " from "\n")
  2063.       (insert "Resent-Date: " (mail-rfc822-date) "\n")
  2064.       ;;>> Insert resent-to: and bcc if need be.
  2065.       (let ((before (point)))
  2066.         (if mail-self-blind
  2067.         (insert "Resent-Bcc: " (user-login-name) "\n"))
  2068.         (insert "Resent-To: " (if (stringp address)
  2069.                    address
  2070.                  (mapconcat 'identity address ",\n\t"))
  2071.             "\n")
  2072.         (save-excursion
  2073.           (expand-mail-aliases before (point))))
  2074.       ;;>> Set up comment, if any.
  2075.       (if (and (sequencep comment) (not (zerop (length comment))))
  2076.           (let ((before (point))
  2077.             after)
  2078.         (insert comment)
  2079.         (or (eolp) (insert "\n"))
  2080.         (setq after (point))
  2081.         (goto-char before)
  2082.         (while (< (point) after)
  2083.           (insert "Resent-Comment: ")
  2084.           (forward-line 1))))
  2085.       ;; Don't expand aliases in the destination fields
  2086.       ;; of the original message.
  2087.       (let (mail-aliases)
  2088.         (funcall send-mail-function)))
  2089.       (kill-buffer tembuf))
  2090.     (rmail-set-attribute "resent" t rmail-current-message)))
  2091.  
  2092. (defvar mail-unsent-separator
  2093.   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
  2094.       "^ *---+ +Returned message +---+ *$\\|"
  2095.       "^ *---+ +Original message +---+ *$\\|"
  2096.       "^ *--+ +begin message +--+ *$\\|"
  2097.       "^ *---+ +Original message follows +---+ *$\\|"
  2098.       "^|? *---+ +Message text follows: +---+ *|?$")
  2099.   "A regexp that matches the separator before the text of a failed message.")
  2100.  
  2101. (defun rmail-retry-failure ()
  2102.   "Edit a mail message which is based on the contents of the current message.
  2103. For a message rejected by the mail system, extract the interesting headers and
  2104. the body of the original message.
  2105. The variable `mail-unsent-separator' should match the string that
  2106. delimits the returned original message."
  2107.   (interactive)
  2108.   (require 'mail-utils)
  2109.   (let (to subj irp2 cc orig-message)
  2110.     (save-excursion
  2111.       ;; Narrow down to just the quoted original message
  2112.       (rmail-beginning-of-message)
  2113.       (let ((case-fold-search t))
  2114.     (or (re-search-forward mail-unsent-separator nil t)
  2115.         (error "Cannot parse this as a failure message")))
  2116.       (save-restriction
  2117.     (let ((old-end (point-max)))
  2118.       ;; One message contained a few random lines before the old
  2119.       ;; message header.  The first line of the message started with
  2120.       ;; two hyphens.  A blank line follows these random lines.
  2121.       (skip-chars-forward "\n")
  2122.       (if (looking-at "^--")
  2123.           (progn
  2124.         (search-forward "\n\n")
  2125.         (skip-chars-forward "\n")))
  2126.       (narrow-to-region (point) (point-max))
  2127.       (goto-char (point-min))
  2128.       (search-forward "\n\n")
  2129.       (narrow-to-region (point-min) (point))
  2130.       ;; Now mail-fetch-field will get from headers of the original message,
  2131.       ;; not from the headers of the rejection.
  2132.       (setq to   (mail-fetch-field "To")
  2133.         subj (mail-fetch-field "Subject")
  2134.         irp2 (mail-fetch-field "In-reply-to")
  2135.         cc   (mail-fetch-field "Cc"))
  2136.       ;; Get the entire text (not headers) of the original message.
  2137.       (goto-char (point-max))
  2138.       (widen)
  2139.       (setq orig-message
  2140.         (buffer-substring (point) old-end)))))
  2141.     ;; Start sending a new message; default header fields from the original.
  2142.     ;; Turn off the usual actions for initializing the message body
  2143.     ;; because we want to get only the text from the failure message.
  2144.     (let (mail-signature
  2145.       (mail-setup-hook rmail-retry-setup-hook))
  2146.       (if (rmail-start-mail nil to subj irp2 cc (current-buffer))
  2147.       ;; Insert original text as initial text of new draft message.
  2148.       (progn
  2149.         (goto-char (point-max))
  2150.         (insert orig-message)
  2151.         (goto-char (point-min))
  2152.         (end-of-line))))))
  2153.  
  2154. (defun rmail-bury ()
  2155.   "Bury current Rmail buffer and its summary buffer."
  2156.   (interactive)
  2157.   ;; This let var was called rmail-buffer, but that interfered
  2158.   ;; with the buffer-local var used in summary buffers.
  2159.   (let ((buffer-to-bury (current-buffer)))
  2160.     (if (rmail-summary-exists)
  2161.     (let (window)
  2162.       (while (setq window (get-buffer-window rmail-summary-buffer))
  2163.         (set-window-buffer window (other-buffer rmail-summary-buffer)))
  2164.       (bury-buffer rmail-summary-buffer)))
  2165.     (switch-to-buffer (other-buffer (current-buffer)))
  2166.     (bury-buffer buffer-to-bury)))
  2167.  
  2168. (defun rmail-summary-exists ()
  2169.   "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
  2170. In fact, the non-nil value returned is the summary buffer itself."
  2171.   (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
  2172.        rmail-summary-buffer))
  2173.  
  2174. (defun rmail-summary-displayed ()
  2175.   "t iff in RMAIL buffer and an associated summary buffer is displayed."
  2176.   (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
  2177.  
  2178. (defvar rmail-redisplay-summary nil
  2179.   "*Non-nil means Rmail should show the summary when it changes.
  2180. This has an effect only if a summary buffer exists.")
  2181.  
  2182. (defvar rmail-summary-window-size nil
  2183.   "*Non-nil means specify the height for an Rmail summary window.")
  2184.  
  2185. ;; Put the summary buffer back on the screen, if user wants that.
  2186. (defun rmail-maybe-display-summary ()
  2187.   (let ((selected (selected-window))
  2188.     window)
  2189.     ;; If requested, make sure the summary is displayed.
  2190.     (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
  2191.      rmail-redisplay-summary
  2192.      (if (get-buffer-window rmail-summary-buffer 0)
  2193.          ;; It's already in some frame; show that one.
  2194.          (let ((frame (window-frame
  2195.                (get-buffer-window rmail-summary-buffer 0))))
  2196.            (make-frame-visible frame)
  2197.            (raise-frame frame))
  2198.        (display-buffer rmail-summary-buffer)))
  2199.     ;; If requested, set the height of the summary window.
  2200.     (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
  2201.      rmail-summary-window-size
  2202.      (setq window (get-buffer-window rmail-summary-buffer))
  2203.      ;; Don't try to change the size if just one window in frame.
  2204.      (not (eq window (frame-root-window (window-frame window))))
  2205.      (unwind-protect 
  2206.          (progn
  2207.            (select-window window)
  2208.            (enlarge-window (- rmail-summary-window-size
  2209.                   (window-height))))
  2210.        (select-window selected)))))
  2211.  
  2212. ;;;; *** Rmail Specify Inbox Files ***
  2213.  
  2214. (autoload 'set-rmail-inbox-list "rmailmsc"
  2215.   "Set the inbox list of the current RMAIL file to FILE-NAME.
  2216. This may be a list of file names separated by commas.
  2217. If FILE-NAME is empty, remove any inbox list."
  2218.   t)
  2219.  
  2220. ;;;; *** Rmail Commands for Labels ***
  2221.  
  2222. (autoload 'rmail-add-label "rmailkwd"
  2223.   "Add LABEL to labels associated with current RMAIL message.
  2224. Completion is performed over known labels when reading."
  2225.   t)
  2226.  
  2227. (autoload 'rmail-kill-label "rmailkwd"
  2228.   "Remove LABEL from labels associated with current RMAIL message.
  2229. Completion is performed over known labels when reading."
  2230.   t)
  2231.  
  2232. (autoload 'rmail-next-labeled-message "rmailkwd"
  2233.   "Show next message with LABEL.  Defaults to last label used.
  2234. With prefix argument N moves forward N messages with this label."
  2235.   t)
  2236.  
  2237. (autoload 'rmail-previous-labeled-message "rmailkwd"
  2238.   "Show previous message with LABEL.  Defaults to last label used.
  2239. With prefix argument N moves backward N messages with this label."
  2240.   t)
  2241.  
  2242. (autoload 'rmail-read-label "rmailkwd"
  2243.   "PROMPT and read with completion an Rmail message label."
  2244.   t)
  2245.  
  2246. ;;;; *** Rmail Edit Mode ***
  2247.  
  2248. (autoload 'rmail-edit-current-message "rmailedit"
  2249.   "Edit the contents of the current message"
  2250.   t)
  2251.  
  2252. ;;;; *** Rmail Sorting ***
  2253.  
  2254. (autoload 'rmail-sort-by-date "rmailsort"
  2255.   "Sort messages of current Rmail file by date.
  2256. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  2257.  
  2258. (autoload 'rmail-sort-by-subject "rmailsort"
  2259.   "Sort messages of current Rmail file by subject.
  2260. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  2261.  
  2262. (autoload 'rmail-sort-by-author "rmailsort"
  2263.   "Sort messages of current Rmail file by author.
  2264. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  2265.  
  2266. (autoload 'rmail-sort-by-recipient "rmailsort"
  2267.   "Sort messages of current Rmail file by recipient.
  2268. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  2269.  
  2270. (autoload 'rmail-sort-by-correspondent "rmailsort"
  2271.   "Sort messages of current Rmail file by other correspondent.
  2272. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  2273.  
  2274. (autoload 'rmail-sort-by-lines "rmailsort"
  2275.   "Sort messages of current Rmail file by number of lines.
  2276. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  2277.  
  2278. (autoload 'rmail-sort-by-keywords "rmailsort"
  2279.   "Sort messages of current Rmail file by labels.
  2280. If prefix argument REVERSE is non-nil, sort them in reverse order.
  2281. KEYWORDS is a comma-separated list of labels." t)
  2282.  
  2283. ;;;; *** Rmail Summary Mode ***
  2284.  
  2285. (autoload 'rmail-summary "rmailsum"
  2286.   "Display a summary of all messages, one line per message."
  2287.   t)
  2288.  
  2289. (autoload 'rmail-summary-by-labels "rmailsum"
  2290.   "Display a summary of all messages with one or more LABELS.
  2291. LABELS should be a string containing the desired labels, separated by commas."
  2292.   t)
  2293.  
  2294. (autoload 'rmail-summary-by-recipients "rmailsum"
  2295.   "Display a summary of all messages with the given RECIPIENTS.
  2296. Normally checks the To, From and Cc fields of headers; but if PRIMARY-ONLY
  2297. is non-nil (prefix arg given), only look in the To and From fields.
  2298. RECIPIENTS is a string of regexps separated by commas."
  2299.   t)
  2300.  
  2301. (autoload 'rmail-summary-by-regexp "rmailsum"
  2302.   "Display a summary of all messages according to regexp REGEXP.
  2303. If the regular expression is found in the header of the message
  2304. \(including in the date and other lines, as well as the subject line),
  2305. Emacs will list the header line in the RMAIL-summary."
  2306.   t)
  2307.  
  2308. (autoload 'rmail-summary-by-topic "rmailsum"
  2309.   "Display a summary of all messages with the given SUBJECT.
  2310. Normally checks the Subject field of headers;
  2311. but if WHOLE-MESSAGE is non-nil (prefix arg given), 
  2312.  look in the whole message.
  2313. SUBJECT is a string of regexps separated by commas."
  2314.   t)
  2315.  
  2316. ;;;; *** Rmail output messages to files ***
  2317.  
  2318. (autoload 'rmail-output-to-rmail-file "rmailout"
  2319.   "Append the current message to an Rmail file named FILE-NAME.
  2320. If the file does not exist, ask if it should be created.
  2321. If file is being visited, the message is appended to the Emacs
  2322. buffer visiting that file."
  2323.   t)
  2324.  
  2325. (autoload 'rmail-output "rmailout"
  2326.   "Append this message to Unix mail file named FILE-NAME."
  2327.   t)
  2328.  
  2329. (autoload 'rmail-output-menu "rmailout"
  2330.   "Output current message to another Rmail file, chosen with a menu."
  2331.   t)
  2332.  
  2333. ;;;; *** Rmail undigestification ***
  2334.  
  2335. (autoload 'undigestify-rmail-message "undigest"
  2336.   "Break up a digest message into its constituent messages.
  2337. Leaves original message, deleted, before the undigestified messages."
  2338.   t)
  2339.  
  2340. (provide 'rmail)
  2341.  
  2342. ;;; rmail.el ends here
  2343.